![](/img/trans.png)
[英]How to open an application on active display while using Process.Start()?
[英]Google Chrome cannot open from application using Process.Start()
我已经用C#编写了Windows应用程序,该应用程序会在标签单击事件时在默认浏览器中打开网站。 我的默认浏览器是Google Chrome。 因此,当我启动应用程序并单击标签时,它无法在Chrome中打开网站。 Chrome抛出错误“无法创建数据目录-Google Chrome无法读取和写入其数据目录”。 我正在使用“ System.Diagnostics.Process.Start”启动进程。
一件有趣的事是,当我关闭应用程序并重新打开它时。 它会在默认的Chrome浏览器中成功打开网站。 :)
我尝试使用Process.StartInfo的各种选项,但从未成功。 以下是用于运行网站的方法。
我正在使用Windows 8-64Bit和.Net Framework4。请在这方面帮助我。
测试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/");
}
我会尝试将WorkingDirectory添加到您的ProcessStartInfo类中
proc.StartInfo.FileName = Path.GetFileName(RegDefaultBrowserPath);
proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(RegDefaultBrowserPath);
将完整文件名传递到FileName属性中不足以为启动的文件设置当前工作目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.