How to find default web browser using C#?

前端 未结 7 1049
长发绾君心
长发绾君心 2020-11-27 05:07

Is there a way I can find out the name of my default web browser using C#? (Firefox, Google Chrome, etc..)

Can you please show me with an example?

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 05:50

    You can look here for an example, but mainly it can be done like this:

    internal string GetSystemDefaultBrowser()
    {
        string name = string.Empty;
        RegistryKey regKey = null;
    
        try
        {
            //set the registry key we want to open
            regKey = Registry.ClassesRoot.OpenSubKey("HTTP\\shell\\open\\command", false);
    
            //get rid of the enclosing quotes
            name = regKey.GetValue(null).ToString().ToLower().Replace("" + (char)34, "");
    
            //check to see if the value ends with .exe (this way we can remove any command line arguments)
            if (!name.EndsWith("exe"))
                //get rid of all command line arguments (anything after the .exe must go)
                name = name.Substring(0, name.LastIndexOf(".exe") + 4);
    
        }
        catch (Exception ex)
        {
            name = string.Format("ERROR: An exception of type: {0} occurred in method: {1} in the following module: {2}", ex.GetType(), ex.TargetSite, this.GetType());
        }
        finally
        {
            //check and see if the key is still open, if so
            //then close it
            if (regKey != null)
                regKey.Close();
        }
        //return the value
        return name;
    
    }
    

提交回复
热议问题