【发布时间】:2013-10-18 05:15:24
【问题描述】:
我用 c# 编写了 windows 应用程序,在标签单击事件时在默认浏览器中打开网站。我的默认浏览器是谷歌浏览器。因此,当我启动我的应用程序并单击标签时,它无法在 Chrome 中打开网站。 Chrome 抛出错误“无法创建数据目录 - Google Chrome 无法读取和写入其数据目录”。我正在使用“System.Diagnostics.Process.Start”来启动进程。
一件有趣的事情是,当我关闭我的应用程序并重新打开它时。它成功地在默认 Chrome 浏览器中打开网站。 :)
我尝试使用 Process.StartInfo 的各种选项,但从未成功。下面是运行网站的方法。
我正在使用 Windows 8-64Bit 和 .Net Framework 4。请你帮我解决这个问题。
测试1:
public static void RunWebClient()
{
Process proc = new Process();
Microsoft.Win32.RegistryKey subKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command");
String DefaultBrowser = subKey.GetValue(null).ToString();
if (DefaultBrowser != null)
{
int startIndex = DefaultBrowser.IndexOf("\"") + 1;
int endIndex = DefaultBrowser.IndexOf("\"", startIndex);
string RegDefaultBrowserPath = DefaultBrowser.Substring(startIndex, endIndex - startIndex);
proc.StartInfo.FileName = RegDefaultBrowserPath;
proc.StartInfo.Arguments = @"http://localhost/Test/";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.LoadUserProfile = false;
proc.Start();
}
}
测试2:
public static void RunWebClient()
{
System.Diagnostics.Process.Start("http://localhost/Test/");
}
【问题讨论】:
-
我不确定这是否相关,但在利用
ShellExecute时,这行proc.StartInfo.FileName = RegDefaultBrowserPath;不是必需的。此外,我怀疑将LoadUserProfile设置为false。只需使用 shell 启动 URL,其余部分不用管。 -
你有没有开启UAC之类的东西?
-
对于路径操作,使用
System.IO.Path类,而不是简单的字符串函数。 -
@neoistheone proc.StartInfo.FileName 必须提供,否则会抛出错误“无法启动进程,因为未提供文件名”。没错,LoadUserProfile 不是必需的,但我认为它可能会导致 chrome 数据目录问题,所以我使用了它。
-
@lan 我机器的 UAC 设置为默认
标签: c# google-chrome process web