C# Arguments for a specific process, open browser with url

后端 未结 1 669
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 01:47

I am writing an application that is supposed to open a certain process on the click of a button. However, the user has the ability to add new buttons. I\'m using the following c

1条回答
  •  醉梦人生
    2021-01-28 02:42

    To start a process to open the browser with a specific url you can try this:

    string url = "http://www.stackoverflow.com";
    
    var process = System.Diagnostics.Process.Start(url);
    

    But sometimes if you have problems with the path of your browser, it cannot work properly. The function bellow gives you the path of the browser in the machine.

    public static string GetDefaultBrowserPath()
    {
        string urlAssociation = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http";
        string browserPathKey = @"$BROWSER$\shell\open\command";
    
        RegistryKey userChoiceKey = null;
        string browserPath = “”;
    
        try
        {
            //Read default browser path from userChoiceLKey
            userChoiceKey = Registry.CurrentUser.OpenSubKey(urlAssociation + @"\UserChoice", false);
    
            //If user choice was not found, try machine default
            if (userChoiceKey == null)
            {
                //Read default browser path from Win XP registry key
                var browserKey = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
    
                //If browser path wasn’t found, try Win Vista (and newer) registry key
                if (browserKey == null)
                {
                    browserKey =
                    Registry.CurrentUser.OpenSubKey(
                    urlAssociation, false);
                }
                var path = CleanifyBrowserPath(browserKey.GetValue(null) as string);
                browserKey.Close();
                return path;
            }
            else
            {
                // user defined browser choice was found
                string progId = (userChoiceKey.GetValue("ProgId").ToString());
                userChoiceKey.Close();
    
                // now look up the path of the executable
                string concreteBrowserKey = browserPathKey.Replace(“$BROWSER$”, progId);
                var kp = Registry.ClassesRoot.OpenSubKey(concreteBrowserKey, false);
                browserPath = CleanifyBrowserPath(kp.GetValue(null) as string);
                kp.Close();
                return browserPath;
            }
        }
        catch(Exception ex)
        {
            return "";  
        }
    }
    

    And you can use the path of the browser and the url of website, for sample:

    string url = "http://www.stackoverflow.com";
    
    var process = System.Diagnostics.Process.Start(GetDefaultBrowserPath(), url);
    

    In the url string you can pass the webpage link. It will open the browser with the url.

    See more:

    http://www.seirer.net/blog/2014/6/10/solved-how-to-open-a-url-in-the-default-browser-in-csharp

    0 讨论(0)
提交回复
热议问题