System.Windows.Forms.WebBrowserのブラウザレンダリングバージョンの変更

System.Windows.Forms.WebBrowserは、デフォルト状態ではIE7相当。

確認サイト:User Agent String.Com

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.useragentstring.com/");
        }
    }
}

 

IE11に変更する場合は、レジストリ「HKEY_CURRENT_USERSOFTWAREMicrosoftInternet ExplorerMainFeatureControlFEATURE_BROWSER_EMULATION」に

プロセス名と対応するデータを登録する。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private const string strRegPath = @"SoftwareMicrosoftInternet ExplorerMainFeatureControlFEATURE_BROWSER_EMULATION";
        private Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(strRegPath, true);
        private string strProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            regKey.SetValue(strProcessName, 11001, Microsoft.Win32.RegistryValueKind.DWord);

            webBrowser1.Navigate("http://www.useragentstring.com/");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            regKey.DeleteValue(strProcessName);
            regKey.Close();
        }
    }
}

 

レジストリに登録するデータを変更すると、他のレンダリングバージョンに変更することも可能。

MSDN Internet Feature Controls (B..C) (Internet Explorer)

 

また、レジストリを使用せず、NavigateメソッドのadditionalHeadersに、直接UserAgentを指定する方法もある。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private const string strUserAgent = @"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.useragentstring.com/", "_self", null, strUserAgent);
        }
    }
}