Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Windows Forms does not open default browser after installation

I have a Windows Forms application (C#, NET 3.5) installed using an MSI installer. In this application I have a button that when pressed opens a browser with a specific URL. I use

Process.Start(url);

to open the browser. This works fine when debugging, but after the installation it has less than optimal results. For example.

  • If I install it with the Just Me options selected i opens my default browser (FF) with current settings.
  • If I install it with the Everyone option, when I press the button it opens a version of IE with out any of my recent settings (proxy, toolbars displayed etc)

As far as I can tell this issue is caused by the user associated with the application when installing.

Taking into account that may users require proxies and personal browser settings and that the Just Me, Everyone choice should remain up to the user. What is the best course o action?

I tried calling Process.Start(url) with the current logged in user using

ProcessStartInfo.UserName = Environment.UserName

But it also requires a password and asking for credentials is not an option.

Do you have any other suggestions, am I using Process.Start() incorrectly, are there settings I need to make during installation, is there anything I missed?

UPDATE: Using Process Explorer as data_smith suggested I noticed the following:

  • If I install the application for Everyone it will start under the NT AUTHORITY\SYSTEM user hence the unconfigured browser.
  • If I install the application with Just Me selected it starts under the current user

Is there a way, without asking for credentials, to make the application start (at windows boot) under the current user even though it is installed for everyone?

UPDATE: Following a suggestion by data_smith to use ShellExecute and the suggestions here and here I was able to solve the problem and get the desired behavior.

The main issue was that when the installer finished the application was started with Process.Start(); This started the application as the NT AUTHORITY\SYSTEM user (the users installers run under) therefore all browsers opened by this application would also be under SYSTEM user. By using the suggestion from data_smith and the suggestions linked above I was able to start the process under the current user.

After the computer is rebooted the application starts under the correct user as this is configured through registry entries.

like image 543
Constantin Avatar asked Apr 20 '12 13:04

Constantin


1 Answers

I recommend accessing the registry to determine the default browser.

//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("");

I tried using this code, and discovered that my registry key ended with "-- \"%1\"".
I don't know why it was there, but I recommend using the following loop to ensure that the key ends in the right place.

//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;
        }
    } 
}
like image 80
Jacklynn Avatar answered Sep 30 '22 00:09

Jacklynn