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 自动化提供程序从 ...
随机推荐
- Docker 容器shell
版权所有,未经许可,禁止转载 章节 Docker 介绍 Docker 和虚拟机的区别 Docker 安装 Docker Hub Docker 镜像(image) Docker 容器(container ...
- Channel详解
复制自:http://www.cnblogs.com/youngKen/p/4921092.html java.nio.channels.FileChannel封装了一个文件通道和一个FileChan ...
- 第一部分 JavaScript语言核心(三)
第六章 对象 P123 在ES3中,点运算符后的标识符不能是保留字.如果一个对象的属性名是保留字,name必须使用方括号的形式访问它们,如o["for"]和o["clas ...
- UVA - 1001 Say Cheese(奶酪里的老鼠)(flod)
题意:无限大的奶酪里有n(0<=n<=100)个球形的洞.你的任务是帮助小老鼠A用最短的时间到达小老鼠O所在位置.奶酪里的移动速度为10秒一个单位,但是在洞里可以瞬间移动.洞和洞可以相交. ...
- C语言-浮点类型
C语言-浮点类型 浮点类型 在0的两侧有一小块区域,这个区域非常接近0,但是不等于0,是float(表达范围数量级10^-38^)或者double(达范围数量级10^-308^)无法表达的,而0是可以 ...
- JDK8中的新特性
1.lambda表达式 1.定义 Java 8 发布的最重要新特性.Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中),可以推导出来的就可以省略了,Lambda 表达式免去了使用匿 ...
- 数据库连接池C3P0的使用
一.直接使用代码链接(一般企业开发不会用到.大多数用方法二) 1.导入jar 2.新建JDBCUtil import java.io.FileInputStream; import java.io.I ...
- 关于javascript中this 指向的4种调用模式
this指向问题绝对可以排js 的top 5最难和最重点的问题,初学者常常搞不清楚this指向哪里,特别是学过java和c#的人,想当年俺也迷糊了好久,直到遇到蝴蝶书,主要是因为js和主流的面向对象语 ...
- 对input type=file 修改样式
效果图先给: 在html中涉及到文件选择的问题,文件选择使用 input(class="filter_input form-control" type="file) 但是 ...
- 1, vm: PropTypes.instanceOf(VM).isRequired
子模块的文件引入父工程对象时,出现红色warning,提示传入的对象类型不是所要求的类型. 思路是父工程引用的JS包和子模块使用的包不是同一个包,解决办法是父工程和子工程都使用同一个包. resolv ...