Detect IE version from a WinForms application

2019-06-16 07:00发布

Is it possible the detect the version of IE installed on a machine from a WinForms application?

EDIT 1

I'm specifically interested in establishing if IE9 is installed or not. Multiple IE versions could be installed, but it is IE9 specifically that causes my application a problem.

5条回答
Fickle 薄情
2楼-- · 2019-06-16 07:12

This is how to get the non-embedded browser-Version:

public static int GetBrowserVersion()
{
    // string strKeyPath = @"HKLM\SOFTWARE\Microsoft\Internet Explorer";
    string strKeyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer";
    string[] ls = new string[] { "svcVersion", "svcUpdateVersion", "Version", "W2kVersion" };

    int maxVer = 0;
    for(int i = 0; i < ls.Length; ++i)
    {
        object objVal = Microsoft.Win32.Registry.GetValue(strKeyPath, ls[i], "0");
        string strVal = System.Convert.ToString(objVal);
        if (strVal != null)
        {
            int iPos = strVal.IndexOf('.');
            if (iPos > 0)
                strVal = strVal.Substring(0, iPos);

            int res = 0;
            if (int.TryParse(strVal, out res))
                maxVer = Math.Max(maxVer, res);
        } // End if (strVal != null)

    } // Next i

    return maxVer;
} // End Function GetBrowserVersion 

Then you can set the embedded browser-Version: 32-bit:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

For 64-bit:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

as a new 32-Bit DWORD key named "yourapp.exe".

// FixBrowserVersion("<YourAppName>", 9000);
public static void FixBrowserVersion(string appName, int lvl)
{
    FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".exe", lvl);
    FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".exe", lvl);
    FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".vshost.exe", lvl);
    FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".vshost.exe", lvl);
}


private static void FixBrowserVersion_Internal(string root, string appName, int lvl)
{
    try
    {

        Microsoft.Win32.Registry.SetValue(root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, lvl);
    }
    catch (Exception)
    {
        // some config will hit access rights exceptions
        // this is why we try with both LOCAL_MACHINE and CURRENT_USER
    }
}

You can use HKLM and HKCU as root. Use HKCU if no admin rights.

See here and here for further Information.

e.g.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION]
"MyApp.exe"=dword:0000270f

Putting it all together:

EmbeddedBrowserHelper.FixBrowserVersion();

with this class

public class EmbeddedBrowserHelper 
{


    public enum BrowserVersion : int
    {
        IE7 = 7000, // 0x1B58
        IE8 = 8888, // 0x22B8
        IE9 = 9999, // 0x270F
        IE10 = 10001, // 0x2AF7
        IE11 = 11001, // 0x2EDF
        IE12 = 12001,
    } // End Enum BrowserVersion


    public static int GetEmbVersion()
    {
        int ieVer = GetBrowserVersion();

        if (ieVer > 9)
            return ieVer * 1000 + 1;

        if (ieVer > 7)
            return ieVer * 1111;

        return 7000;
    } // End Function GetEmbVersion



    public static void FixBrowserVersion()
    { 
        string appName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);
        FixBrowserVersion(appName);
    }


    public static void FixBrowserVersion(string appName)
    {
        FixBrowserVersion(appName, GetEmbVersion());
    } // End Sub FixBrowserVersion


    // FixBrowserVersion("<YourAppName>", 9000);
    public static void FixBrowserVersion(string appName, int ieVer)
    {
        FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".exe", ieVer);
        FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".exe", ieVer);
        FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".vshost.exe", ieVer);
        FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".vshost.exe", ieVer);
    } // End Sub FixBrowserVersion 


    private static void FixBrowserVersion_Internal(string root, string appName, int ieVer)
    {
        try
        {
            Microsoft.Win32.Registry.SetValue(root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, ieVer);
        }
        catch (Exception)
        {
            // some config will hit access rights exceptions
            // this is why we try with both LOCAL_MACHINE and CURRENT_USER
        }
    } // End Sub FixBrowserVersion_Internal 


    public static int GetBrowserVersion()
    {
        // string strKeyPath = @"HKLM\SOFTWARE\Microsoft\Internet Explorer";
        string strKeyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer";
        string[] ls = new string[] { "svcVersion", "svcUpdateVersion", "Version", "W2kVersion" };

        int maxVer = 0;
        for (int i = 0; i < ls.Length; ++i)
        {
            object objVal = Microsoft.Win32.Registry.GetValue(strKeyPath, ls[i], "0");
            string strVal = System.Convert.ToString(objVal);
            if (strVal != null)
            {
                int iPos = strVal.IndexOf('.');
                if (iPos > 0)
                    strVal = strVal.Substring(0, iPos);

                int res = 0;
                if (int.TryParse(strVal, out res))
                    maxVer = Math.Max(maxVer, res);
            } // End if (strVal != null)

        } // Next i

        return maxVer;
    } // End Function GetBrowserVersion 


}
查看更多
beautiful°
3楼-- · 2019-06-16 07:14

You can determe the Internet Explorer version from the registry:

HKLM\SOFTWARE\Microsoft\Internet Explorer\Version

See also: Determine the version of Internet Explorer installed on a local machine

查看更多
Emotional °昔
4楼-- · 2019-06-16 07:23

Look at the File Version for iexplore.exe. If you are worried about multiple versions installed, check the one used in the file associations for html files.

查看更多
趁早两清
5楼-- · 2019-06-16 07:26

How about;

string ver = (new WebBrowser()).Version.ToString();
查看更多
Melony?
6楼-- · 2019-06-16 07:35

In a winform application there another issue which has not been mentioned. Even if IE9 is installed the webbrowser is always running with the IE7.0 engine.

If you want your application to benefit from a more recent html renderer you got to write in the registry. The code below works for me. That is:

  • in and out of Visual studio
  • whether the app user has administrative rights or not.

    FixBrowserVersion("<YourAppName>", 9000);
    
    private static void FixBrowserVersion(string appName, int lvl)
    {
        FixBrowserVersion2("HKEY_CURRENT_USER", appName+".exe", lvl);
        FixBrowserVersion2("HKEY_LOCAL_MACHINE", appName+".exe", lvl);
        FixBrowserVersion2("HKEY_CURRENT_USER", appName+".vshost.exe", lvl);
        FixBrowserVersion2("HKEY_LOCAL_MACHINE", appName + ".vshost.exe", lvl);
    }
    
    private static void FixBrowserVersion2(string root, string appName, int lvl)
    {
        try
        {
            Registry.SetValue(root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, lvl);
        }
        catch (Exception) 
        {
             // some config will hit access rights exceptions
             // this is why we try with both LOCAL_MACHINE and CURRENT_USER
        }
    }
    
查看更多
登录 后发表回答