admin管理员组

文章数量:1516870

WebBrowser脚本错误的原因及Web Browser Control & Specifying the IE Version
实际是IE打开版本的问题
WebBrowse默认打开IE7.0的版本
32 bit:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
Value Key: yourapplication.exe
64 bit:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

The value to set this key to is (taken from MSDN here) as decimal values:

9999 (0x270F)
Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.

9000 (0x2328)
Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.

8888 (0x22B8)
Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.

8000 (0x1F40)
Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.

7000 (0x1B58)
Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.

最近想用 WebBrowser控件实际打开发生报错

解决方案一(手动修改注册表)

32位系统运行32位程序或64位系统运行64位程序,在这里修改:

代码实现更改IE版本(注册表)

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Microsoft.Win32;using System.Management;namespace RegistryVs
{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();try{
                is64 =GetBit();InitIEdic();GetIEValue(out IENum,out IEPath,out maxIeVesion,out maxIeValue, is64);foreach(KeyValuePair<int, RadioButton> item in ibr){if(item.Key > IENum){
                        item.Value.Visible =false;}if(item.Key == IENum){
                        item.Value.BackColor = lbBest.BackColor;}}if(is64 ==true){
                    IEBrowserPath =@"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";}else{
                    IEBrowserPath =@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";}
                openFileDialog1.Filter ="|*.exe";
 
                lbCurIE.Text = maxIeVesion;}catch(Exception ex){
                MessageBox.Show(ex.Source,"操作提示");}}publicbool is64 =false;publicint IENum =7;publicstring maxIeVesion;publicUInt16 maxIeValue;publicstring IEPath;publicstring IEBrowserPath;privateboolGetBit(){try{string addressWidth = String.Empty;ConnectionOptions mConnOption =newConnectionOptions();ManagementScope mMs =newManagementScope("//localhost", mConnOption);ObjectQuery mQuery =newObjectQuery("select AddressWidth from Win32_Processor");ManagementObjectSearcher mSearcher =newManagementObjectSearcher(mMs, mQuery);ManagementObjectCollection mObjectCollection = mSearcher.Get();foreach(ManagementObject mObject in mObjectCollection){
                    addressWidth = mObject["AddressWidth"].ToString();}if(addressWidth =="32"){returnfalse;}else{returntrue;}}catch(Exception ex){returnfalse;}}privateUInt16GetSetIeVersion(){int ienum =7;;foreach(KeyValuePair<int, RadioButton> item in ibr){if(item.Value.Visible ==true&& item.Value.Checked ==true){
                    ienum = item.Key;}}return IeDic[ienum];}publicvoidGetPath(outstring path,outstring filename){string fullname =this.textBox1.Text;
            fullname = fullname.Replace("/","\\");int index = fullname.LastIndexOf('\\');
            path = fullname.Substring(0, index);
            filename = fullname.Substring(index   1, fullname.Length - index -1);}privatevoidbutton1_Click(object sender,EventArgs e){//RegisterObj r = new RegisterObj();string path;string name;GetPath(out path,out name);if(!name.EndsWith(".exe")){
                name = name   ".exe";}//string sb;//string p2;//if (is64 == true)//{//    //sb = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths";//    p2 = @"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";//}//else//{//    //sb = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";//    p2 = @"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";//}//r.CreateSubKey(sb   @"\"   name, RegDomain.LocalMachine);//RegisterObj r2 = new RegisterObj(sb   @"\"   name, RegDomain.LocalMachine);//if (r2.IsSubKeyExist())//{//    r2.WriteRegeditKey("Path", path);//    r2.WriteRegeditKey("", path   @"\"   name);//}RegisterObj r3 =newRegisterObj(IEBrowserPath, RegDomain.LocalMachine);if(IeNull.Checked ==true){
                r3.DeleteRegeditKey(name);
                lbIeSet.Text ="无";}else{UInt16 setIeVersion =GetSetIeVersion();
                r3.WriteRegeditKey(name, setIeVersion, RegValueKind.DWord);
                lbIeSet.Text ="IE"   VerDic[setIeVersion];}
 
 
 
            MessageBox.Show("注册成功");}privatevoidGetIEValue(outint ieNum,outstring iePath,outstring version,outUInt16 ie,bool is64){if(is64){
                iePath =@"software\Wow6432Node\Microsoft\Internet Explorer";}else{
                iePath =@"software\Microsoft\Internet Explorer";}RegistryKey mreg;
            mreg = Microsoft.Win32.Registry.LocalMachine;
            mreg = mreg.CreateSubKey(iePath);
            version =(String)mreg.GetValue("Version");string[] vs = version.ToString().Split('.');if(vs.Length >0){
                ieNum = Convert.ToInt32(vs[0]);}else{
                ieNum =7;}if(ieNum >=7&& ieNum <=11){}else{
                ieNum =7;}
 
            ie = IeDic[ieNum];
 
 
            mreg.Close();}privatevoidbutton2_Click(object sender,EventArgs e){}privatevoidbutton3_Click(object sender,EventArgs e){
            openFileDialog1.ShowDialog();}private Dictionary<int, UInt16> IeDic =newDictionary<int,UInt16>();public Dictionary<UInt16,int> VerDic =newDictionary<UInt16,int>();public Dictionary<RadioButton,int> rbi =newDictionary<RadioButton,int>();public Dictionary<int, RadioButton> ibr =newDictionary<int,RadioButton>();publicvoidAddDic(int ie,UInt16 ver,RadioButton rb){
            IeDic.Add(ie, ver);
            VerDic.Add(ver, ie);
            rbi.Add(rb, ie);
            ibr.Add(ie, rb);}publicvoidInitIEdic(){/*
                11001 (0x2EDF) Internet Explorer 11. Webpages are displayed in IE11 Standards mode, regardless of the !DOCTYPE directive
                11000 (0x2AF8) :Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode
                10000 (0x2710) :Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
                10001 (0x2AF7) :Internet Explorer 10. Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive.
                9999 (0x270F) :Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
                9000 (0x2328) :Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
                8888 (0x22B8) :Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.
                8000 (0x1F40) :Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.
                7000 (0x1B58) :Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.
             * */AddDic(7,7000, rdIE7);AddDic(8,8888, rdIE8);AddDic(9,9999, rdIE9);AddDic(10,10001, rdIE10);AddDic(11,11001, rdIE11);}privatevoidopenFileDialog1_FileOk(object sender,CancelEventArgs e){if(!openFileDialog1.FileName.EndsWith(".exe")){
                MessageBox.Show("请选择主程序注册,必须为exe文件");this.textBox1.Text ="";
                lbIeSet.Text ="无";return;}else{this.textBox1.Text = openFileDialog1.FileName;RegisterObj r3 =newRegisterObj(IEBrowserPath, RegDomain.LocalMachine);string path;string name;GetPath(out path,out name);if(!name.EndsWith(".exe")){
                    name = name   ".exe";}bool exists = r3.IsRegeditKeyExist(name);if(exists ==true){UInt16 curversion = Convert.ToUInt16(r3.ReadRegeditKey(name));
                    lbIeSet.Text =GetIeString(curversion);int ienum = VerDic[curversion];if(ibr.ContainsKey(ienum)&& ibr[ienum].Visible ==true){
                        ibr[ienum].Checked =true;}else{
                        IeNull.Checked =true;}}else{
                    ibr[7].Checked =true;
 
                    lbIeSet.Text ="IE7";}}}publicstringGetIeString(UInt16 iedbversion){if(VerDic.ContainsKey(iedbversion)){return"IE"   VerDic[iedbversion];}else{return"未知";}}privatevoidForm1_Load(object sender,EventArgs e){}}}

代码更改二(在软件加载开始时候formload中执行)

/// <summary>/// 定义IE版本的枚举/// </summary>privateenum IeVersion
{
    强制ie10,//10001 (0x2711) Internet Explorer 10。网页以IE 10的标准模式展现,页面!DOCTYPE无效
    标准ie10,//10000 (0x02710) Internet Explorer 10。在IE 10标准模式中按照网页上!DOCTYPE指令来显示网页。Internet Explorer 10 默认值。
    强制ie9,//9999 (0x270F) Windows Internet Explorer 9. 强制IE9显示,忽略!DOCTYPE指令
    标准ie9,//9000 (0x2328) Internet Explorer 9. Internet Explorer 9默认值,在IE9标准模式中按照网页上!DOCTYPE指令来显示网页。
    强制ie8,//8888 (0x22B8) Internet Explorer 8,强制IE8标准模式显示,忽略!DOCTYPE指令
    标准ie8,//8000 (0x1F40) Internet Explorer 8默认设置,在IE8标准模式中按照网页上!DOCTYPE指令展示网页
    标准ie7//7000 (0x1B58) 使用WebBrowser Control控件的应用程序所使用的默认值,在IE7标准模式中按照网页上!DOCTYPE指令来展示网页}/// <summary>/// 设置WebBrowser的默认版本/// </summary>/// <param name="ver">IE版本</param>privatevoidSetIE(IeVersion ver){string productName = AppDomain.CurrentDomain.SetupInformation.ApplicationName;//获取程序名称object version;switch(ver){case IeVersion.标准ie7:
            version =0x1B58;break;case IeVersion.标准ie8:
            version =0x1F40;break;case IeVersion.强制ie8:
            version =0x22B8;break;case IeVersion.标准ie9:
            version =0x2328;break;case IeVersion.强制ie9:
            version =0x270F;break;case IeVersion.标准ie10:
            version =0x02710;break;case IeVersion.强制ie10:
            version =0x2711;break;default:
            version =0x1F40;break;}RegistryKey key = Registry.CurrentUser;RegistryKey software =
        key.CreateSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\" + productName);
    if (software != null)
    {
        software.Close();
        software.Dispose();
    }
    RegistryKey wwui =
        key.OpenSubKey(
            @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",true);//该项必须已存在if(wwui !=null) wwui.SetValue(productName, version, RegistryValueKind.DWord);}

本文标签: 标准标准模式强制