C# 设备管理器交互发送密钥
我正在尝试编写一个简单的控制台程序,该程序将启动设备管理器,并通过按键在其中导航。到目前为止,我无法让他们在设备管理器中注册 该程序可以启动设备管理器,但在设备管理器中似乎没有按键。我知道通过使用Spy++,设备管理器的树部分被称为SysTreeView32 有什么建议吗 这是到目前为止我的代码C# 设备管理器交互发送密钥,c#,ui-automation,sendkeys,findwindow,C#,Ui Automation,Sendkeys,Findwindow,我正在尝试编写一个简单的控制台程序,该程序将启动设备管理器,并通过按键在其中导航。到目前为止,我无法让他们在设备管理器中注册 该程序可以启动设备管理器,但在设备管理器中似乎没有按键。我知道通过使用Spy++,设备管理器的树部分被称为SysTreeView32 有什么建议吗 这是到目前为止我的代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Di
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
namespace Dev_Mgr_Auto
{
class Program
{
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lp1, string lp2);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
static void Main(string[] args)
{
//start Device Manager
Process.Start("devmgmt.msc");
Thread.Sleep(1500);
// find window handle of Device manager
IntPtr handle = FindWindow("MMCMainFrame", "Device Manager");
if (!handle.Equals(IntPtr.Zero))
{
// activate Device Danager window
if (SetForegroundWindow(handle))
{
// send key "Tab"
SendKeys.SendWait("{TAB}");
// send key "Down" x 4
SendKeys.SendWait("DOWN 4");
}
}
}//end main
}//end class
}// end program
EDIT1:您必须以管理员身份运行该程序才能正常工作。
最终代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Automation;
namespace Video_Card_Updater
{
class Program
{
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lp1, string lp2);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
static void Main(string[] args)
{
// this code needs a reference to UIAutomationClient and UIAutomationTypes
Process process = Process.Start("devmgmt.msc");
do
{
process.Refresh();
Thread.Sleep(100);
}
while (process.MainWindowHandle == IntPtr.Zero);
// get root element that corresponds to the process main window
AutomationElement mainWindow = AutomationElement.FromHandle(process.MainWindowHandle);
// get the first tree view control by its class name
AutomationElement treeView = mainWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, "SysTreeView32"));
// get the "Keyboards" node by its name
AutomationElement Display = treeView.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "Display adapters"));
// expand item
((ExpandCollapsePattern)Display.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand();
// get first display
AutomationElement firstDisplay = Display.FindFirst(TreeScope.Children, PropertyCondition.TrueCondition);
// set focus to display and do the following key commands:
firstDisplay.SetFocus();
SendKeys.SendWait("{ENTER}");
Thread.Sleep(100);
SendKeys.SendWait("{TAB 3}");
Thread.Sleep(100);
SendKeys.SendWait("{RIGHT}");
Thread.Sleep(100);
//ALT + P
SendKeys.SendWait("%P");
Thread.Sleep(100);
SendKeys.SendWait("{ENTER}");
} // end main
}//end class
}// end program
我建议你改用这项技术。它适应了这种情况,下面是一个示例,它打开“Keyboards”节点,选择第一个键盘节点并模拟按ENTER键:
static void Main(string[] args)
{
// this code needs a reference to UIAutomationClient and UIAutomationTypes
Process process = Process.Start("devmgmt.msc");
do
{
process.Refresh();
Thread.Sleep(100);
}
while (process.MainWindowHandle == IntPtr.Zero);
// get root element that corresponds to the process main window
AutomationElement mainWindow = AutomationElement.FromHandle(process.MainWindowHandle);
// get the first tree view control by its class name
AutomationElement treeView = mainWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, "SysTreeView32"));
// get the "Keyboards" node by its name
AutomationElement keyBoards = treeView.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "Keyboards"));
// expand item
((ExpandCollapsePattern)keyBoards.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand();
// get first keyboard
AutomationElement firstKeyboard = keyBoards.FindFirst(TreeScope.Children, PropertyCondition.TrueCondition);
// open the first keyboard properties (focus + press ENTER)
firstKeyboard.SetFocus();
SendKeys.SendWait("{ENTER}");
}
一旦您选择了一个项目,您想做什么?我想最终选择“显示驱动程序”,点击“更新驱动程序软件”,然后选择“自动搜索更新的软件”。我可以这样做所有这些击键,但由于某种原因,它们不会在设备管理器中注册。我的程序中必须有哪个引用才能使用它?哦,错过了,它给了我一个NullReferenceUnhandled错误:对象引用未设置为对象的实例。在这一行
((ExpandCollapsePattern)keyBoards.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand()代码>也许你的设备管理器没有一个名为“Keyboards”的节点。它有一个节点,大小写都一样。奇怪。也许在两次通话之间睡一觉。或者尝试其他节点名称。