我遇到了同样的问题,WebBrowser使用的是旧版本的IE,通过一些搜索,我遇到了以下代码,这些代码更改了注册表,并使WebBrowser可以使用最新的IE版本:
public enum BrowserEmulationVersion
{
Default = 0,
Version7 = 7000,
Version8 = 8000,
Version8Standards = 8888,
Version9 = 9000,
Version9Standards = 9999,
Version10 = 10000,
Version10Standards = 10001,
Version11 = 11000,
Version11Edge = 11001
}
public static class WBEmulator
{
private const string InternetExplorerRootKey = @"Software\Microsoft\Internet Explorer";
public static int GetInternetExplorerMajorVersion()
{
int result;
result = 0;
try
{
RegistryKey key;
key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey);
if (key != null)
{
object value;
value = key.GetValue("svcVersion", null) ?? key.GetValue("Version", null);
if (value != null)
{
string version;
int separator;
version = value.ToString();
separator = version.IndexOf('.');
if (separator != -1)
{
int.TryParse(version.Substring(0, separator), out result);
}
}
}
}
catch (SecurityException)
{
// The user does not have the permissions required to read from the registry key.
}
catch (UnauthorizedAccessException)
{
// The user does not have the necessary registry rights.
}
return result;
}
private const string BrowserEmulationKey = InternetExplorerRootKey + @"\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
public static BrowserEmulationVersion GetBrowserEmulationVersion()
{
BrowserEmulationVersion result;
result = BrowserEmulationVersion.Default;
try
{
RegistryKey key;
key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
if (key != null)
{
string programName;
object value;
programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
value = key.GetValue(programName, null);
if (value != null)
{
result = (BrowserEmulationVersion)Convert.ToInt32(value);
}
}
}
catch (SecurityException)
{
// The user does not have the permissions required to read from the registry key.
}
catch (UnauthorizedAccessException)
{
// The user does not have the necessary registry rights.
}
return result;
}
public static bool SetBrowserEmulationVersion(BrowserEmulationVersion browserEmulationVersion)
{
bool result;
result = false;
try
{
RegistryKey key;
key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
if (key != null)
{
string programName;
programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
if (browserEmulationVersion != BrowserEmulationVersion.Default)
{
// if it's a valid value, update or create the value
key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord);
}
else
{
// otherwise, remove the existing value
key.DeleteValue(programName, false);
}
result = true;
}
}
catch (SecurityException)
{
// The user does not have the permissions required to read from the registry key.
}
catch (UnauthorizedAccessException)
{
// The user does not have the necessary registry rights.
}
return result;
}
public static bool SetBrowserEmulationVersion()
{
int ieVersion;
BrowserEmulationVersion emulationCode;
ieVersion = GetInternetExplorerMajorVersion();
if (ieVersion >= 11)
{
emulationCode = BrowserEmulationVersion.Version11;
}
else
{
switch (ieVersion)
{
case 10:
emulationCode = BrowserEmulationVersion.Version10;
break;
case 9:
emulationCode = BrowserEmulationVersion.Version9;
break;
case 8:
emulationCode = BrowserEmulationVersion.Version8;
break;
default:
emulationCode = BrowserEmulationVersion.Version7;
break;
}
}
return SetBrowserEmulationVersion(emulationCode);
}
public static bool IsBrowserEmulationSet()
{
return GetBrowserEmulationVersion() != BrowserEmulationVersion.Default;
}
}
您只需要创建一个类并将此代码放入其中,然后在程序启动时运行以下代码:
if (!WBEmulator.IsBrowserEmulationSet())
{
WBEmulator.SetBrowserEmulationVersion();
}
VB.NET:
Imports Microsoft.Win32
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Security
Imports System.Text
Imports System.Threading.Tasks
Public Enum BrowserEmulationVersion
[Default] = 0
Version7 = 7000
Version8 = 8000
Version8Standards = 8888
Version9 = 9000
Version9Standards = 9999
Version10 = 10000
Version10Standards = 10001
Version11 = 11000
Version11Edge = 11001
End Enum
Public Class WBEmulator
Private Const InternetExplorerRootKey As String = "Software\Microsoft\Internet Explorer"
Public Shared Function GetInternetExplorerMajorVersion() As Integer
Dim result As Integer
result = 0
Try
Dim key As RegistryKey
key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey)
If key IsNot Nothing Then
Dim value As Object = If(key.GetValue("svcVersion", Nothing), key.GetValue("Version", Nothing))
Dim Version As String
Dim separator As Integer
Version = value.ToString()
separator = Version.IndexOf(".")
If separator <> -1 Then
Integer.TryParse(Version.Substring(0, separator), result)
End If
End If
Catch ex As SecurityException
'The user does Not have the permissions required to read from the registry key.
Catch ex As UnauthorizedAccessException
'The user does Not have the necessary registry rights.
Catch
End Try
GetInternetExplorerMajorVersion = result
End Function
Private Const BrowserEmulationKey = InternetExplorerRootKey + "\Main\FeatureControl\FEATURE_BROWSER_EMULATION"
Public Shared Function GetBrowserEmulationVersion() As BrowserEmulationVersion
Dim result As BrowserEmulationVersion
result = BrowserEmulationVersion.Default
Try
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True)
If key IsNot Nothing Then
Dim programName As String
Dim value As Object
programName = Path.GetFileName(Environment.GetCommandLineArgs()(0))
value = key.GetValue(programName, Nothing)
If value IsNot Nothing Then
result = CType(Convert.ToInt32(value), BrowserEmulationVersion)
End If
End If
Catch ex As SecurityException
'The user does Not have the permissions required to read from the registry key.
Catch ex As UnauthorizedAccessException
'The user does Not have the necessary registry rights.
Catch
End Try
GetBrowserEmulationVersion = result
End Function
Public Shared Function SetBrowserEmulationVersion(BEVersion As BrowserEmulationVersion) As Boolean
Dim result As Boolean = False
Try
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True)
If key IsNot Nothing Then
Dim programName As String = Path.GetFileName(Environment.GetCommandLineArgs()(0))
If BEVersion <> BrowserEmulationVersion.Default Then
'if it's a valid value, update or create the value
key.SetValue(programName, CType(BEVersion, Integer), RegistryValueKind.DWord)
Else
'otherwise, remove the existing value
key.DeleteValue(programName, False)
End If
result = True
End If
Catch ex As SecurityException
' The user does Not have the permissions required to read from the registry key.
Catch ex As UnauthorizedAccessException
' The user does Not have the necessary registry rights.
End Try
SetBrowserEmulationVersion = result
End Function
Public Shared Function SetBrowserEmulationVersion() As Boolean
Dim ieVersion As Integer
Dim emulationCode As BrowserEmulationVersion
ieVersion = GetInternetExplorerMajorVersion()
If ieVersion >= 11 Then
emulationCode = BrowserEmulationVersion.Version11
Else
Select Case ieVersion
Case 10
emulationCode = BrowserEmulationVersion.Version10
Case 9
emulationCode = BrowserEmulationVersion.Version9
Case 8
emulationCode = BrowserEmulationVersion.Version8
Case Else
emulationCode = BrowserEmulationVersion.Version7
End Select
End If
SetBrowserEmulationVersion = SetBrowserEmulationVersion(emulationCode)
End Function
Public Shared Function IsBrowserEmulationSet() As Boolean
IsBrowserEmulationSet = GetBrowserEmulationVersion() <> BrowserEmulationVersion.Default
End Function
End Class
您可以像这样使用它:
If Not WBEmulator.IsBrowserEmulationSet() Then
WBEmulator.SetBrowserEmulationVersion()
End If