解决IE当前脚本发生错误异常

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位程序,在这里修改:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
64位系统下的32位程序,在HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION 添加自己应用程序DWORD键,值改为2AF9。
在这里插入图片描述

代码实现更改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
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            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, "操作提示");
            }
 
        }
 
 
        public bool is64 = false;
 
        public int IENum = 7;
 
        public string maxIeVesion;
 
        public UInt16 maxIeValue;
 
        public string IEPath;
 
 
        public string IEBrowserPath;
 
        private bool GetBit()
        {
            try
            {
                string addressWidth = String.Empty;
                ConnectionOptions mConnOption = new ConnectionOptions();
                ManagementScope mMs = new ManagementScope("//localhost", mConnOption);
                ObjectQuery mQuery = new ObjectQuery("select AddressWidth from Win32_Processor");
                ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mMs, mQuery);
                ManagementObjectCollection mObjectCollection = mSearcher.Get();
                foreach (ManagementObject mObject in mObjectCollection)
                {
                    addressWidth = mObject["AddressWidth"].ToString();
                }
                if (addressWidth == "32")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
 
        private UInt16 GetSetIeVersion()
        {
            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];
        }
 
 
        public void GetPath(out string path, out string 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);
        }
 
        private void button1_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 = new RegisterObj(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("注册成功");
        }
 
        private void GetIEValue(out int ieNum, out string iePath, out string version, out UInt16 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();
        }
 
 
        private void button2_Click(object sender, EventArgs e)
        {
 
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
 
        }
 
 
        private Dictionary<int, UInt16> IeDic = new Dictionary<int, UInt16>();
 
        public Dictionary<UInt16, int> VerDic = new Dictionary<UInt16, int>();
 
 
        public Dictionary<RadioButton, int> rbi = new Dictionary<RadioButton, int>();
 
        public Dictionary<int, RadioButton> ibr = new Dictionary<int, RadioButton>();
 
        public void AddDic(int ie, UInt16 ver, RadioButton rb)
        {
            IeDic.Add(ie, ver);
            VerDic.Add(ver, ie);
            rbi.Add(rb, ie);
            ibr.Add(ie, rb);
        }
 
        public void InitIEdic()
        {
 
            /*
                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);
 
        }
 
        private void openFileDialog1_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 = new RegisterObj(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";
 
                }
 
            }
        }
 
        public string GetIeString(UInt16 iedbversion)
        {
            if (VerDic.ContainsKey(iedbversion))
            {
                return "IE"   VerDic[iedbversion];
            }
            else
            {
                return "未知";
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
    }
}

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

/// <summary>
/// 定义IE版本的枚举
/// </summary>
private enum 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>
private void SetIE( 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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值