我正在尝试从 .NET 程序启动 Google Chrome 浏览器,并带有参数。但我的行为很奇怪。
以下内容从命令行以“incognito”模式启动 Chrome。效果很好。
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --incognito
但是以下内容在.NET 中不起作用。 Chrome 确实打开了,但不是隐身模式,它会转到这个奇怪的 URL:
http://xn---incognito-nu6e/
Module Module1
Sub Main()
System.Diagnostics.Process.Start("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "–-incognito")
End Sub
End Module
调用
chrome.exe
时可以使用快捷方式,而不是使用完整路径位置。
Module Module1
Sub Main()
System.Diagnostics.Process.Start("chrome.exe", "--incognito")
End Sub
End Module
更多:start-google-chrome-from-run-windows-key-r
更新
我发现你的代码有什么问题。 您的代码在参数中使用
–-incognito
,但它应该是 --incognito
。
查看该参数中的第一个字符。应该是
-
(连字符减号:U+002D)而不是 –
(En Dash/U+2013)。
Module Module1
Sub Main()
System.Diagnostics.Process.Start("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "--incognito")
End Sub
End Module
您还可以从注册表中读取chrome路径:
public static string GetChromePath()
{
string lPath = null;
try
{
var lTmp = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe", "", null);
if (lTmp != null)
lPath = lTmp.ToString();
else
{
lTmp = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe", "", null);
if (lTmp != null)
lPath = lTmp.ToString();
}
}
catch (Exception lEx)
{
Logger.Error(lEx);
}
if (lPath == null)
{
Logger.Warn("Chrome install path not found! Returning hardcoded path");
lPath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
}
return lPath;
}