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 自动化提供程序从 ...
随机推荐
- js基础学习之-js全局对象
声明的三种方式: 第一种: var test; //或var test = 5; 第二种: test = 5; 第三种: window.test; //或window.test = 5; //只是使用 ...
- Nginx php-fpm 分离搭建 (上) 未完
最近又重新看了一遍 'nginx入门到精通' 抽点时间 出来搭几个Demo 会有更深体会: Nginx如何与Php-fpm结合 Nginx不只有处理http请求的功能,还能做反向代理. ...
- 读书笔记 - js高级程序设计 - 第十二章 DOM2和DOM3
Node类型的变化 访问元素的样式 myDiv.style.backgroundColor = "red" myDiv.style.width = "100px& ...
- Q3狂揽3亿美元净利润的特斯拉会让国内电动汽车厂商喜极而泣吗?
作为电动汽车行业的标杆,特斯拉无疑是国内电动汽车厂商发展进程中重要的参考对象.而前段时间特斯拉身上出现的产能受阻.私有化风波.马斯克卸任董事长一职等事件,着实让国产电动汽车厂商惊出一身冷汗.毕竟如果特 ...
- Go语言开发环境的搭建(Goland和VSCode)
教程首发于:微信公众号<Go编程时光>,欢迎你一起来学习 1. 下载安装 Go语言 下载地址:https://golang.google.cn/dl/ 下载完成后,直接双击 msi 文件进 ...
- JS元素的左右移动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- SDN(Software Defined Network):软件定义网络----转载
SDN(Software Defined Network):软件定义网络 传统的网络转发行为: 1)逐设备单独控制,纯分布式控制. 2)控制面和转发面在同一个设备中,耦合紧密. 管理员无法直接操控转发 ...
- 第四章:Schema与数据类型优化
1. 选择优化的数据类型 选择数据类型的原则 更小的通常更好:选择可以正确存储数据的最小数据类型 小的数据类型消耗更少的内存.CPU;占用更少的磁盘 选用简单的数据类型:简单的数据类型通常需要更少的C ...
- Tensorflow学习教程------参数保存和提取重利用
#coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mni ...
- JZOJ-2019-11-5 A组
T1 给定由 n 个点 m 条边组成的无向连通图,保证没有重边和自环. 你需要找出所有边,满足这些边恰好存在于一个简单环中.一个环被称为简单环,当且仅当它包含的所有点都只在这个环中被经过了一次.(即求 ...