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

时间:2012-04-20 13:28:41

标签: c# windows winforms

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

Process.Start(url);

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

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

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

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

我尝试使用

与当前登录用户调用Process.Start(url)
ProcessStartInfo.UserName = Environment.UserName

但它也需要密码,并且不能选择凭证。

您有任何其他建议,我是否正确使用Process.Start(),是否需要在安装过程中进行设置,是否有任何遗漏?

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

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

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

UPDATE:根据data_smith的建议使用ShellExecute以及建议herehere我能够解决问题并获得所需的行为。

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

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

2 个答案:

答案 0 :(得分: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("");

我尝试使用此代码,发现我的注册表项以“ - \”%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;
        }
    } 
}

答案 1 :(得分:0)

using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication13
{
    public partial class Form1 : Form
    {
        public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
        // Add a link to the LinkLabel.
        LinkLabel.Link link = new LinkLabel.Link();
        link.LinkData = "http://www.dotnetperls.com/";
        linkLabel1.Links.Add(link);
    }

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        // Send the URL to the operating system.
        Process.Start(e.Link.LinkData as string);
    }
    }
}
相关问题