UI Automation技术获取cmd或Powershell命令提示符窗口的实时内容
事先打开的Powershell或cmd窗口中的文本,用其他方式难以拿到。但是用UI Automation可以轻松获取。
本工具在窗体上加入了一个Timer控件,每秒钟都查找桌面上是否有Powershell或cmd窗口,如果有就获取文本区域的内容。
代码如下: using System;
using System.Windows.Automation;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace PowerShellTracker_CS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void timer1_Tick(object sender, EventArgs e)
{
PropertyCondition PC1 = new PropertyCondition(property: AutomationElement.ControlTypeProperty, value: ControlType.Window);
PropertyCondition PC2 = new PropertyCondition(property: AutomationElement.ClassNameProperty, value: "ConsoleWindowClass");
AutomationElement PS = AutomationElement.RootElement.FindFirst(scope: TreeScope.Children, condition: new AndCondition(PC1,PC2));
if (PS == null)
{}
else
{PropertyCondition PC3 = new PropertyCondition(property: AutomationElement.NameProperty, value: "Text Area");
PropertyCondition PC4 = new PropertyCondition(property: AutomationElement.ControlTypeProperty, value: ControlType.Document);
AutomationElement TA = PS.FindFirst(scope: TreeScope.Children, condition: new AndCondition(PC3, PC4));
if(TA == null)
{
textBox1.Text = "未找到窗口。";
}
else
{
TextPattern TP = (TextPattern)TA.GetCurrentPattern(TextPattern.Pattern);
System.Windows.Automation.Text.TextPatternRange TR = TP.DocumentRange;
string Source= TR.GetText(-);
MatchCollection MC= Regex.Matches(Source, ".+");
Source ="";
string LastLine = "";
foreach(Match match in MC)
{
if (match.Value == "\r")
{ }
else
{
Source += match.Value + "\r\n";
LastLine = match.Value;
}
}
textBox1.Text = Source + "最后一行是:" + LastLine;
textBox1.SelectionStart = textBox1.TextLength;
textBox1.ScrollToCaret();
}
} }
}
}
对应的VB.NET代码如下:
Imports System.Windows.Automation
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim PC1 As PropertyCondition = New PropertyCondition([property]:=AutomationElement.ControlTypeProperty, value:=ControlType.Window)
Dim PC2 As PropertyCondition = New PropertyCondition([property]:=AutomationElement.ClassNameProperty, value:="ConsoleWindowClass")
Dim PS As AutomationElement = AutomationElement.RootElement.FindFirst(scope:=TreeScope.Children, condition:=New AndCondition(PC1, PC2)) If PS Is Nothing Then
Else
Dim PC3 As PropertyCondition = New PropertyCondition([property]:=AutomationElement.NameProperty, value:="Text Area")
Dim PC4 As PropertyCondition = New PropertyCondition([property]:=AutomationElement.ControlTypeProperty, value:=ControlType.Document)
Dim TA As AutomationElement = PS.FindFirst(scope:=TreeScope.Children, condition:=New AndCondition(PC3, PC4)) If TA Is Nothing Then
TextBox1.Text = "未找到窗口。"
Else
Dim TP As TextPattern = CType(TA.GetCurrentPattern(TextPattern.Pattern), TextPattern)
Dim TR As Text.TextPatternRange = TP.DocumentRange
Dim Source As String = TR.GetText(-)
Dim MC As MatchCollection = Regex.Matches(Source, ".+")
Source = ""
Dim LastLine As String = ""
For Each match As Match In MC
If match.Value = vbCr Then
Else
Source += match.Value & vbCrLf
LastLine = match.Value
End If
Next
TextBox1.Text = Source & "最后一行是:" & LastLine
TextBox1.SelectionStart = TextBox1.TextLength
TextBox1.ScrollToCaret()
End If
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With Me.Timer1
.Interval =
.Enabled = True
End With
End Sub
End Class

UI Automation技术获取cmd或Powershell命令提示符窗口的实时内容的更多相关文章
- MS UI Automation Introduction
		
MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...
 - 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt
		
首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...
 - Python之uiautomation模块-获取CMD窗口中所打印的文字信息
		
当我们想以自动化的方式操作软件,以提高办公或测试效率时,有许多成熟的工具,比如针对Web端应用的Selenium.针对移动端应用的Appium.那么,PC端(Windows)桌面应用,又改如何处理呢? ...
 - UI Automation 简介
		
转载,源地址: http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interf ...
 - 从UI Automation看Windows平台自动化测试原理
		
前言 楼主在2013年初研究Android自动化测试的时候,就分享了几篇文章 Android ViewTree and DecorView Android自动化追本溯源系列(1): 获取页面元素 An ...
 - MS UI Automation简介
		
转自:http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interface A ...
 - 使用UI Automation实现自动化测试--1-4
		
Introduction UI Automation是Microsoft .NET 3.0框架下提供的一种用于自动化测试的技术,是在MSAA基础上建立的,MSAA就是Microsoft Active ...
 - 使用UI Automation实现自动化测试 --工具使用
		
当前项目进行三个多月了,好久也没有写日志了:空下点时间,补写下N久没写的日志 介绍下两个工具 我本人正常使用的UISpy.exe工具和inspect.exe工具 这是UISPY工具使用的图,正常使用到 ...
 - Server-Side UI Automation Provider - WinForm Sample
		
Server-Side UI Automation Provider - WinForm Sample 2014-09-14 源代码 目录 引用程序集提供程序接口公开服务器端 UI 自动化提供程序从 ...
 
随机推荐
- spring boot集成mybatis(1)
			
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
 - Codeforces 392 B Blown Garland
			
题意:输入是由连续的RYGB和字符!组成的字符串,R代表红灯,Y代表黄灯,G代表绿灯,B代表蓝灯.简而言之,就是给定的字符串保证了下标对4取余相同的灯颜色都相同,但是有的地方为‘!’代表这个灯坏了,然 ...
 - DataStructuresAndAlogorithm--红黑树
			
简介 为了理解红黑树(red-black tree)是什么,首先需要知道二叉树. 定义1:二叉树是结点的有限集合,该集合或者为空集,或者是由一个根和两棵互不相交的,称为该根的左子树和右子树的二叉树组成 ...
 - 【LeetCode】解码方法
			
[问题] 一条包含字母 A-Z 的消息通过以下方式进行了编码:'A' -> 1'B' -> 2…'Z' -> 26给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 : 输 ...
 - 如何保障Assignment写作效率?
			
有没有因为开学要交的Assignment而日夜赶工.身心俱疲啊?写Assignment确实是个体力+脑力活,要一直保持旺盛的精力并不容易.精神和身体的疲劳会慢慢分散你的注意力,进而影响效率和写作质量. ...
 - CountDownLatch、CyclicBarrier、Semaphore的使用
			
CountDownLatch(计数器) 主线程等待另外三个线程执行完成后再执行 public static void main(String[] args) { //定义一个CountDownLatc ...
 - mongodb与sql语句对比
			
左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * from users db.users.find({"age" ...
 - 题解 P1220 【关路灯】
			
区间DP, 考虑设\(dp[i][j][t]\)为已经关掉了\([i,j]\)的电灯, 人在t端点处时的最小代价 可以推出方程: \[ dp[i+1][j][0]+(p[n]-p[j]+p[i])*( ...
 - VS2017中使用ReportViewer控件,vs2017找不到Microsoft Rdlc Report Designer for Visual Studio
			
VS2017中没有ReportViewer控件,这个控件用来实现在项目中显示和打印关系数据库中的表比较容易,特别是想要打印的时候,这个比用DataGridView和PrintDocument要简单一些 ...
 - 【每日Scrum】第三天冲刺
			
一.计划会议内容 登录和个人主界面开发布局实现. 二.任务看板 三.scrum讨论照片 四.产品的状态 登录与个人界面布局实现 五.任务燃尽图