6

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.

Community
  • 1
  • 1
Constantin
  • 465
  • 4
  • 18
  • Do you run your application from installer? – ogggre Apr 20 '12 at 13:32
  • After the application is installed it gets started immediately and it always starts at machine startup. It sits in the system tray. – Constantin Apr 20 '12 at 13:36
  • 2
    I think what @ogggre asked was, is your application launched by the installer? This could be a "credentials"-related problem. Use ProcExp (by SysInternals) to see the parent of you program. – data Apr 20 '12 at 15:51
  • 1
    Also, did you try [ShellExecute](http://pinvoke.net/default.aspx/shell32/ShellExecute.html)? – data Apr 20 '12 at 15:52
  • @data_smith: Please formulate an answer for me to accept with your ShellExecute suggestion as it solved my problem as described in the last update. – Constantin May 07 '12 at 13:40

2 Answers2

1

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;
        }
    } 
}
Jacklynn
  • 1,503
  • 1
  • 13
  • 23
  • As stated in the question the problem is not finding the default browser, it is opening the browser as the current user. – Constantin Apr 23 '12 at 10:26
  • "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)" -- According to this, Constantin, the problem clearly is finding the default browser associated with the current user. – Jacklynn Apr 29 '12 at 22:21
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);
    }
    }
}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
mj-gholami
  • 883
  • 1
  • 6
  • 19
  • 1
    I've made a standalone application that does exactly this and it behaves as desired. The problem only arises when the application is bundled with an installer and installed for everyone. – Constantin Apr 23 '12 at 10:30