简体   繁体   English

Google Chrome无法使用Process.Start()从应用程序打开

[英]Google Chrome cannot open from application using Process.Start()

I have written windows application in c# that opens website in default browser on label click event. 我已经用C#编写了Windows应用程序,该应用程序会在标签单击事件时在默认浏览器中打开网站。 My default browser is Google Chrome. 我的默认浏览器是Google Chrome。 So when I start my application and click on label, it fails to open website in Chrome. 因此,当我启动应用程序并单击标签时,它无法在Chrome中打开网站。 Chrome throws error "Failed to create data directory - Google Chrome cannot read and write to its data directory". Chrome抛出错误“无法创建数据目录-Google Chrome无法读取和写入其数据目录”。 I am using "System.Diagnostics.Process.Start" to start process. 我正在使用“ System.Diagnostics.Process.Start”启动进程。

One interesting thing is, when I close my application and reopen it. 一件有趣的事是,当我关闭应用程序并重新打开它时。 It sucessfully opens website in default Chrome browser. 它会在默认的Chrome浏览器中成功打开网站。 :) :)

I tried by using various options of Process.StartInfo but never succeed. 我尝试使用Process.StartInfo的各种选项,但从未成功。 Below is the method used to run website. 以下是用于运行网站的方法。

I am using Windows 8-64Bit and .Net Framework 4. Could you please help me on this. 我正在使用Windows 8-64Bit和.Net Framework4。请在这方面帮助我。

Test1: 测试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();
    }
}

Test2: 测试2:

public static void RunWebClient()
{
    System.Diagnostics.Process.Start("http://localhost/Test/");
}

I would try to add the WorkingDirectory to your ProcessStartInfo class 我会尝试将WorkingDirectory添加到您的ProcessStartInfo类中

proc.StartInfo.FileName = Path.GetFileName(RegDefaultBrowserPath);
proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(RegDefaultBrowserPath);

Passing the full file name into the FileName property is not enough to set the current working directory for the launched file. 将完整文件名传递到FileName属性中不足以为启动的文件设置当前工作目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM