原贴在此:http://www.cnblogs.com/doscanner/p/5611434.html
WebBrowser内核指定
一、背景
这几天在维护公司的一个项目,嗯…到现在七八年没人动过了(也是老古董了),都说N年前的代码碰不得 处处是坑 不能挖坑还得一步一步的填坑,恰好今天就填了一坑 此处作为记录 供以后翻阅,对代码除了有些看不懂或者说是很凌乱之外,其他都还行(没注释、有注释的地方是自动生成的英文注释…..、包含 各种委托、事件、多线程、用户控件等等)。
二、问题
对 就是一个CS程序,其中有个功能是输入网址然后展示网页内容,so问题来了 很多网站不支持IE8以下内核的浏览器(很粗暴的跳转到浏览器下载页面~~)。那为什么不用webkit内核的呢…刚刚也说了这是七八年前的代码了…,用的是WebBrowser WebBrowser WebBrowser,说给换一个吧 时间又不允许 而且…你懂的~~(主要是调整太多),也就只能死马当活马医了。
三、解决方式
1.升级IE浏览器
第一时间 想到的就是升级IE浏览器,说干就干 下载安装最新版IE浏览器…重启…运行程序…输入网址… 很粗暴的跳转到浏览器下载页面~~,看来还是太天真… 原来WebBrowser默认使用IE7内核、只升级浏览器是没用的,需要手动指定
2.手动指定WebBrowser内核
浏览了许多资料后,找到了解决方案:改注册表
在开始菜单内输入“regedit.exe”,进入注册表编辑器
找到注册表项:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
在右侧空白区域内单击鼠标右键,点击[新建]→[DWORD(32-位)值]
新建的项取名为MyApplication.exe,编辑值时,选择基数“十进制”,填写数值数据,这里填写11000(IE11)
最后运行MyAplication.exe,发现问题已经解决
不同IE版本所对应的DWORD值:
原文:https://msdn.microsoft.com/en-us/library/ee330730(v=vs.85).aspx#browser_emulation
但是….这样手动去改注册表始终比较麻烦,本着能用代码解决的问题就尽量用代码来解决的想法,决定改改
3.自动指定WebBrowser内核
思路:检测IE版本、根据不同IE版本返回DWORD值、修改注册表
直接贴代码吧,里面有注释
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
public
class
IEVersion
{
/// <summary>
/// IE WebBrowser内核设置
/// </summary>
public
static
void
BrowserEmulationSet()
{
//当前程序名称
var
exeName = Process.GetCurrentProcess().ProcessName +
".exe"
;
//系统注册表信息
var
mreg = Registry.LocalMachine;
//IE注册表信息
var
ie = mreg.OpenSubKey(
@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"
, RegistryKeyPermissionCheck.ReadWriteSubTree);
if
(ie !=
null
)
{
try
{
var
val = ieVersionEmulation(ieVersion());
if
(val != 0)
{
ie.SetValue(exeName, val);
}
mreg.Close();
}
catch
(Exception ex)
{
Console.Write(ex.Message);
}
}
}
/// <summary>
/// IE版本号
/// </summary>
/// <returns></returns>
static
int
ieVersion()
{
//IE版本号
RegistryKey mreg = Registry.LocalMachine;
mreg = mreg.CreateSubKey(
"SOFTWARE\\Microsoft\\Internet Explorer"
);
//更新版本
var
svcVersion = mreg.GetValue(
"svcVersion"
);
if
(svcVersion !=
null
)
{
mreg.Close();
var
v = svcVersion.ToString().Split(
'.'
)[0];
return
int
.Parse(v);
}
else
{
//默认版本
var
ieVersion = mreg.GetValue(
"Version"
);
mreg.Close();
if
(ieVersion !=
null
)
{
var
v = ieVersion.ToString().Split(
'.'
)[0];
return
int
.Parse(v);
}
}
return
0;
}
/// <summary>
/// 根据IE版本号 返回Emulation值
/// </summary>
/// <param name="ieVersion"></param>
/// <returns></returns>
static
int
ieVersionEmulation(
int
ieVersion)
{
//IE7 7000 (0x1B58)
if
(ieVersion < 8)
{
return
0;
}
if
(ieVersion == 8)
{
return
0x1F40;
//8000 (0x1F40)、8888 (0x22B8)
}
if
(ieVersion == 9)
{
return
0x2328;
//9000 (0x2328)、9999 (0x270F)
}
else
if
(ieVersion == 10)
{
return
0x02710;
//10000 (0x02710)、10001 (0x2711)
}
else
if
(ieVersion == 11)
{
return
0x2AF8;
//11000 (0x2AF8)、11001 (0x2AF9
}
return
0;
}
}
|
最后,在程序启动时调用:
IEVersion.BrowserEmulationSet();
然后检查注册表,发现已经修改成功。再次运行程序,发现问题已经解决。
以上只是本人解决问题的一个方式方法,不官方不权威,不当之处烦请指正
花了一天时间,解决了一个问题(只是暂时的),想想还是挺悲哀的,这工作效率要是被老板知道了…
这是一个很多年前的代码,即使他很古董,但其中必然有我值得学习的地方,这只是第一步,后面还有许多坑要去填,几年下来也确实累积了许多bug,至少曾经还是辉煌过…
当然,会改版的(正决定往Web版调整呢)….
回头看了看自己的博客,惨(zhi)不(guai)忍(tai)睹(lan),后面还是得多添砖加瓦了