C # Windows Forms not opening default browser after installation

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

Process.Start(url); 

to open the browser. This works great when debugging, but after installation has less optimal results. For instance.

  • If I installed it with the Just Me options selected, I will open my default browser (FF) with the current settings.
  • If I install it using the "All" option, when I click the button it opens a version of IE without any of my last settings (proxies, toolbars, etc.)

As far as I can tell, this problem is caused by the user associated with the application during installation.

Whereas users may need proxies and personal browser settings, and Just Just, each user should remain with the user. What is the best course of action?

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

 ProcessStartInfo.UserName = Environment.UserName 

But this also requires a password and requesting credentials is not an option.

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

UPDATE: Using Process Explorer as a data_smith package, I noticed the following:

  • If I install the application for Everyone, it will run under NT AUTHORITY \ SYSTEM, therefore, an unconfigured browser.
  • If I install the application using Just Me, it will start in the current user

Is there a way, without asking for credentials, to run the application (when Windows starts) under the current user, even if it is installed for everyone?

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

The main problem was that when the installation was completed, the program started with Process.Start (); This started the application as the NT AUTHORITY \ SYSTEM user (for users running users), so all browsers opened by this application will also be under the SYSTEM user. Using the sentence from data_smith and the above sentences, I was able to start the process under the current user.

After the computer restarts, the application starts under the correct user, as it is configured through registry entries.

+6
source share
2 answers

I recommend contacting 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 found 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; } } } 
+1
source
 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); } } } 
0
source

Source: https://habr.com/ru/post/913729/


All Articles