본문 바로가기
C#

[C#] Webbrowser - ie 버전 up

by Jcoder 2021. 9. 2.

기존 .NET Framework에서 webbrowser는 IE 7버전이기에 js 오류가 날 수 있어 레지로 버전을 11로 향상 시킬 수 있다.

 

1. 레지등록 방법

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION]
"프로그램이름.exe"=dword:00002ee1

xxx.reg로 만들어서 등록하면 해당 프로그램명은 ie 11로 사용

 

 

2. 프로그램 방법 (관리자 권한으로 실행 필요)

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1_net
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetBrowserEmulationVersion();
            this.webBrowser1.Navigate("");
        }

        private const string InternetExplorerRootKey = @"Software\Microsoft\Internet Explorer";
        private const string BrowserEmulationKey = InternetExplorerRootKey + @"\Main\FeatureControl\FEATURE_BROWSER_EMULATION";

        public enum BrowserEmulationVersion
        {
            Default = 0,
            Version7 = 7000,
            Version8 = 8000,
            Version8Standards = 8888,
            Version9 = 9000,
            Version9Standards = 9999,
            Version10 = 10000,
            Version10Standards = 10001,
            Version11 = 11000,
            Version11Edge = 11001
        }

        #region 현재 설치되어있는 IE Explorer 버전 읽어오는것
        public static int GetInternetExplorerMajorVersion()
        {
            int result = 0;
            try
            {
                RegistryKey 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;
        }
        #endregion

        #region 에뮬레이션 브라우저 버전 읽어오기
        public static BrowserEmulationVersion GetBrowserEmulationVersion()
        {
            BrowserEmulationVersion result = BrowserEmulationVersion.Default;

            try
            {
                RegistryKey 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 IsBrowserEmulationSet() => GetBrowserEmulationVersion() != BrowserEmulationVersion.Default;
        #endregion

        #region 에뮬레이션 브라우저 설정
        public static bool SetBrowserEmulationVersion(BrowserEmulationVersion browserEmulationVersion)
        {
            bool result = false;

            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);

                if (key != null)
                {
                    string 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 = GetInternetExplorerMajorVersion();
            BrowserEmulationVersion emulationCode;

            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);
        }
        #endregion
    }
}