개발일지/c#

c# windowForm web browser version up(C# 브라우저 버전 올리기)

자코린이 2022. 1. 6. 18:51

c# 을 사용해 윈도우 프로그램을 만들고 있다가 예상치 못한 문제를 만났다.

기본적으로 윈도움 폼에서 제공하는 web browser의 버전은 ie8버전이었다. (컴퓨터에 따라 다를 수 있다.)

그래서 html5와 최신 css를 이용한 웹페이지가 안 된다.

하필 프로그램의 개발은 거의 끝난 상황에서 윈도우에서 재공하는 webview2를 이용해 함수를 고칠 수도 없었다.

그래서 버전을 올려야한다는 결론이 나왔다.

만약 이러한 문제를 격는 사람이 있을 수 있어 글을 적는다.

namespace IEVersionUp
{
    public partial class Form1 : Form
    {
        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;
            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;
        }

        #endregion

        #region 에뮬레이션 브라우저 버전 읽어오기

        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 IsBrowserEmulationSet()
        {
            return GetBrowserEmulationVersion() != BrowserEmulationVersion.Default;
        }

        #endregion

        #region 에뮬레이션 브라우저 설정

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

                             //여기부터 코드 작성
}
#endregion

 

이 코드가 도움이 되길 바라며 즐거운 코딩합시다. enjoy your coding :)

'개발일지 > c#' 카테고리의 다른 글

배열에서 LINQ사용  (0) 2022.01.12