我一直在尝试使用以下代码获取Google Chrome活动标签网址。在某些计算机上,例如i7处理器,获取URL大约需要3秒钟。但是,在i5和i3处理器计算机上它的速度要快得多。有没有办法在不到一秒的时间内获取URL?对此的任何帮助都非常感谢。
static void Main(string[] args)
{
Process[] procsChrome = Process.GetProcessesByName("chrome");
if (procsChrome.Length <= 0)
{
Console.WriteLine("Chrome is not running");
}
else
{
foreach (Process proc in procsChrome)
{
if (proc.MainWindowHandle == IntPtr.Zero)
{
continue;
}
AutomationElement elm = AutomationElement.FromHandle(proc.MainWindowHandle);
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
if (elmUrlBar != null)
{
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
Console.WriteLine(val.Current.Value);
}
}
Console.ReadKey();
}
}
}