简体   繁体   中英

How to set IE9 by default for web browser?

I am developing an application in C#.NET. I want to use the IE9 version for WebBrowser; either IE9 is installed on system or not.

Is it possible that using IE9 with WebBrower and it may be that IE9 is not installed in my system?

With Windows Internet Explorer 8 or later the FEATURE_BROWSER_EMULATION feature defines the default emulation mode for Internet Explorer. Value 9999 - forces webpages to be displayed in IE9 Standards mode, regardless of the !DOCTYPE directive. You need IE9 or later installed on the target system. Check Internet Feature Controls (B..C)

private static void WebBrowserVersionEmulation()
{
    const string BROWSER_EMULATION_KEY = 
    @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
    //
    // app.exe and app.vshost.exe
    String appname = Process.GetCurrentProcess().ProcessName + ".exe";
    //
    // Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
    const int browserEmulationMode = 9999;

    RegistryKey browserEmulationKey =
        Registry.CurrentUser.OpenSubKey(BROWSER_EMULATION_KEY,RegistryKeyPermissionCheck.ReadWriteSubTree) ??
        Registry.CurrentUser.CreateSubKey(BROWSER_EMULATION_KEY);

    if (browserEmulationKey != null)
    {
        browserEmulationKey.SetValue(appname, browserEmulationMode, RegistryValueKind.DWord);
        browserEmulationKey.Close();
    }
}

Insert

"<meta  http-equiv=\"X-UA-Compatible\" content=\"IE="\9\" >"

Into your html page,but you have to know Web_browser control dependent on version of IE that already installed on target OS

For this, you have to lookup

  • What is the version of the underlying browser (registry key may return former installed version). Simplest code I use is asking the WebBrowser control

     WebBrowser browser= new WebBrowser Version ver= browser.Version(); // With ver.Major you can decide the EMULATION 

  • The app-exe-name of your app (differs, while running in vs debug environment to "myapp".vshost.exe). This code I found somewhere:

     // This code detects the .vshost. when running in vs ide [DllImport("kernel32.dll", SetLastError=true)] private static extern int GetModuleFileName([In]IntPtr hModule, [Out]StringBuilder lpFilename, [In][MarshalAs(UnmanagedType.U4)] int nSize); public static String getAppExeName() { StringBuilder appname= new StringBuilder(1024); GetModuleFileName(IntPtr.Zero, appname, appname.Capacity); return Path.GetFileName(appname.ToString()); // return filename part } 

  • Now, you can calculate the Registry-Entry which is necessary for the browser compatibility. The Entry may be in Registry.LocalMachine (access rights required) or Registry.CurrentUser. I check the registry on every program start, so, I first test the existence of the entry

     string regSubKey= @"Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION"; string version= "" + ver.Version + "0000"; // installed version x 10000 string appname= getAppExeName(); RegistryKey rs = Registry.CurrentUser.OpenSubKey(regSubKey); keyval = rs.GetValue(appname); rs.Close(); if (keyval != null && keyval.ToString().Equals(version)) return; // already done and no browser update installed. // // Create key for this app and this version rs = Registry.LocalMachine.CreateSubKey(regSubKey); rs.SetValue(app, sversion, RegistryValueKind.DWord); rs.Flush(); rs.Close(); 

  • In 64bit + 32bit modes, may be you have to create an entry in "Software\\Wow6432Node" too

After setting the registry key, the WebBrowser control should start with the required emulation

No, the webbrowser-element (I think you mean this) is on base of IE6. You only can start the process of IE9 (don't know the name but for firefox its simply "firefox.exe") form the program.

You can read the version from the registry:

var ieVersion = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Internet Explorer").GetValue("Version");

or

If you have a WebBrowser control you can grab it from there:

WebBrowser  browser = new WebBrowser();
Version ver = browser.Version;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM