■ Internet Explorer의 브라우저 에뮬레이션 모드를 설정하는 방법을 보여준다.
▶ InternetExplorerHelper.cs
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
using Microsoft.Win32; using System; using System.IO; using System.Reflection; namespace TestProject { /// <summary> /// Internet Explorer 헬퍼 /// </summary> public class InternetExplorerHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 브라우저 에뮬레이션 모드 구하기 - GetBrowserEmulationMode() /// <summary> /// 브라우저 에뮬레이션 모드 구하기 /// </summary> /// <returns>브라우저 에뮬레이션 모드</returns> public static int GetBrowserEmulationMode() { int internetExplorerVersion = GetInternetExplorerVersion(); if(internetExplorerVersion > 9) { return internetExplorerVersion * 1000 + 1; } if(internetExplorerVersion > 7) { return internetExplorerVersion * 1111; } return 7000; } #endregion #region 브라우저 에뮬레이션 모드 설정하기 - SetBrowserEmulationMode(applicationName, browserEmulationMode) /// <summary> /// 브라우저 에뮬레이션 모드 설정하기 /// </summary> /// <param name="applicationName">애플리케이션명</param> /// <param name="browserEmulationMode">브라우저 에뮬레이션 모드</param> public static void SetBrowserEmulationMode(string applicationName, int browserEmulationMode) { SetBrowserEmulationMode("HKEY_LOCAL_MACHINE", $"{applicationName}.exe" , browserEmulationMode); SetBrowserEmulationMode("HKEY_CURRENT_USER" , $"{applicationName}.exe" , browserEmulationMode); SetBrowserEmulationMode("HKEY_LOCAL_MACHINE", $"{applicationName}.vshost.exe", browserEmulationMode); SetBrowserEmulationMode("HKEY_CURRENT_USER" , $"{applicationName}.vshost.exe", browserEmulationMode); } #endregion #region 브라우저 에뮬레이션 모드 설정하기 - SetBrowserEmulationMode(applicationName) /// <summary> /// 브라우저 에뮬레이션 모드 설정하기 /// </summary> /// <param name="applicationName">애플리케이션명</param> public static void SetBrowserEmulationMode(string applicationName) { SetBrowserEmulationMode(applicationName, GetBrowserEmulationMode()); } #endregion #region 브라우저 에뮬레이션 모드 설정하기 - SetBrowserEmulationMode() /// <summary> /// 브라우저 에뮬레이션 모드 설정하기 /// </summary> public static void SetBrowserEmulationMode() { string applicationName = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location); SetBrowserEmulationMode(applicationName); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region Internet Explorer 버전 구하기 - GetInternetExplorerVersion() /// <summary> /// Internet Explorer 버전 구하기 /// </summary> /// <returns>Internet Explorer 버전</returns> private static int GetInternetExplorerVersion() { string keyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer"; string[] valueNameArray = new string[] { "svcVersion", "svcUpdateVersion", "Version", "W2kVersion" }; int maximumVersion = 0; for(int i = 0; i < valueNameArray.Length; i++) { object valueObject = Registry.GetValue(keyPath, valueNameArray[i], "0"); string valueString = Convert.ToString(valueObject); if(valueString != null) { int position = valueString.IndexOf('.'); if(position > 0) { valueString = valueString.Substring(0, position); } if(int.TryParse(valueString, out int version)) { maximumVersion = Math.Max(maximumVersion, version); } } } return maximumVersion; } #endregion #region 브라우저 에뮬레이션 모드 설정하기 - SetBrowserEmulationMode(root, applicationName, browserEmulationMode) /// <summary> /// 브라우저 에뮬레이션 모드 설정하기 /// </summary> /// <param name="root">루트</param> /// <param name="applicationName">애플리케이션명</param> /// <param name="browserEmulationMode">브라우저 에뮬레이션 모드</param> private static void SetBrowserEmulationMode(string root, string applicationName, int browserEmulationMode) { //if(Environment.Is64BitProcess) //{ // Registry.SetValue // ( // root + @"\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", // applicationName, // browserEmulationMode // ); //} //else //{ Registry.SetValue ( root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", applicationName, browserEmulationMode ); //} } #endregion } } |
※ SetBrowserEmulationMode(string root, string applicationName, int browserEmulationMode) 메소드에서 실제로는 32/64비트 프로세스 구분없이 설정해서 사용했다.
▶ Program.cs
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 |
using System; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { int browserEmulationMode = InternetExplorerHelper.GetBrowserEmulationMode(); Console.WriteLine($"브라우저 에뮬레이션 모드 : {browserEmulationMode}"); InternetExplorerHelper.SetBrowserEmulationMode(); } #endregion } } |