C#Windows窗体在安装后无法打开默认浏览器

Con*_*tin 6 c# windows winforms

我有一个使用MSI安装程序安装的Windows窗体应用程序(C#,NET 3.5).在这个应用程序中,我有一个按钮,当按下时打开一个具有特定URL的浏览器.我用

Process.Start(url);
Run Code Online (Sandbox Code Playgroud)

打开浏览器.这在调试时工作正常,但在安装后它的结果不是最佳.例如.

  • 如果我选择Just Me选项安装它,我会打开我当前设置的默认浏览器(FF).
  • 如果我使用Everyone选项安装它,当我按下按钮时,它会打开一个IE版本,不包含我最近的任何设置(代理,工具栏显示等)

据我所知,这个问题是由安装时与应用程序关联的用户引起的.

考虑到用户可能需要代理和个人浏览器设置以及Just Me,Everyone选择应该由用户决定.什么是最好的行动?

我尝试使用当前登录用户调用Process.Start(url)

ProcessStartInfo.UserName = Environment.UserName
Run Code Online (Sandbox Code Playgroud)

但它也需要密码,并且要求凭证不是一种选择.

你有任何其他的建议,我是否正确使用Process.Start(),我在安装过程中需要进行设置,有什么我错过的吗?

更新: 使用Process Explorer作为data_smith建议我注意到以下内容:

  • 如果我为Everyone安装应用程序,它将在NT AUTHORITY\SYSTEM用户下启动,因此未配置的浏览器.
  • 如果我选择Just Me选择安装应用程序,它将在当前用户下启动

有没有办法在没有要求凭据的情况下使应用程序在当前用户下启动(在Windows启动时),即使它是为每个人安装的?

更新:遵循data_smith的建议使用ShellExecute以及此处此处的建议我能够解决问题并获得所需的行为.

主要问题是当安装程序完成后,应用程序启动了Process.Start(); 这启动了应用程序作为NT AUTHORITY\SYSTEM用户(用户安装程序在其下运行)因此,此应用程序打开的所有浏览器也将在SYSTEM用户下.通过使用data_smith的建议和上面链接的建议,我能够在当前用户下启动该过程.

重新启动计算机后,应用程序将在正确的用户下启动,因为这是通过注册表项进行配置的.

Jac*_*ack 1

我建议访问注册表以确定默认浏览器。

//Create a registry key to read the default browser variable
RegistryKey reader = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command");
//Determine the default browser
string DefaultBrowser = (string)reader.GetValue("");
Run Code Online (Sandbox Code Playgroud)

我尝试使用此代码,发现我的注册表项以“--\”%1\“”结尾。
我不知道为什么它在那里,但我建议使用以下循环来确保密钥在正确的位置结束。

//If the path starts with a ", it will end with a "
if (DefaultBrowser[0] == '"')
{
    for (int count = 1; count < DefaultBrowser.Length; count++)
    {
        if (DefaultBrowser[count] == '"')
        {
           DefaultBrowser = DefaultBrowser.Remove(count + 1);
           count = DefaultBrowser.Length + 22;
        }
    }
}
//Otherwise, the path will end with a ' '
else
{
    for (int count = 0; count < DefaultBrowser.Length; count++)
    {
        if (DefaultBrowser[count] == ' ')
        {
           DefaultBrowser = DefaultBrowser.Remove(count + 1);
           count = DefaultBrowser.Length + 22;
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)