示例#1
0
        /// <summary>
        /// Registers the app to use not the quirks mode in browser which would let the auth window fail
        /// See http://www.west-wind.com/weblog/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version for more details
        /// If this registry key is not set the authorization window will fail
        /// </summary>
        /// <param name="whichBrowserToUse">Which type of IE to use (should be at least IE 8 always)</param>
        /// <param name="assemblyName">Name of the .exe (leave empty for autodetect)</param>
        /// <param name="alsoCreateVshostEntry">Will create a second entry with assembly.vshost.exe instead of assembly.exe as this procress is the debugging process in Visual Studio</param>
        public static bool registerAppInRegistry(registerBrowserEmulationValue whichBrowserToUse, string assemblyName = "", bool alsoCreateVshostEntry = true)
        {
            if (whichBrowserToUse == registerBrowserEmulationValue.NoChange)
            {
                return(true);
            }
            Microsoft.Win32.RegistryKey registryKey   = null;
            Microsoft.Win32.RegistryKey registryKey64 = null;
            if (string.IsNullOrEmpty(assemblyName))
            {
                assemblyName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
            }

            registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION");
            if (Environment.Is64BitOperatingSystem)
            {
                registryKey64 = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION");
            }
            if (whichBrowserToUse == registerBrowserEmulationValue.Remove)
            {
                try
                {
                    registryKey.DeleteValue(assemblyName);
                    if (registryKey64 != null)
                    {
                        registryKey64.DeleteValue(assemblyName);
                        return(true);
                    }
                }
                catch
                {
                    return(false);
                }
            }
            else
            {
                int browser_value = 0;
                // browser registry values taken from http://msdn.microsoft.com/en-us/library/ee330730%28v=vs.85%29.aspx#browser_emulation
                switch (whichBrowserToUse)
                {
                case registerBrowserEmulationValue.IE7IfDoctypeAvailable:
                    browser_value = 7000;
                    break;

                case registerBrowserEmulationValue.IE8IfDoctypeAvailable:
                    browser_value = 8000;
                    break;

                case registerBrowserEmulationValue.IE8Always:
                    browser_value = 8888;
                    break;

                case registerBrowserEmulationValue.IE9IfDoctypeAvailable:
                    browser_value = 9000;
                    break;

                case registerBrowserEmulationValue.IE9Always:
                    browser_value = 9999;
                    break;

                case registerBrowserEmulationValue.IE10IfDoctypeAvailable:
                    browser_value = 10000;
                    break;

                case registerBrowserEmulationValue.IE10Always:
                    browser_value = 10001;
                    break;

                default:
                    browser_value = 0;
                    break;
                }
                try
                {
                    registryKey.SetValue(assemblyName, browser_value, Microsoft.Win32.RegistryValueKind.DWord);
                    if (alsoCreateVshostEntry)
                    {
                        registryKey.SetValue(assemblyName.ToLower().Replace(".exe", ".vshost.exe"), browser_value, Microsoft.Win32.RegistryValueKind.DWord);
                    }
                    if (registryKey64 != null)
                    {
                        registryKey64.SetValue(assemblyName, browser_value, Microsoft.Win32.RegistryValueKind.DWord);
                        if (alsoCreateVshostEntry)
                        {
                            registryKey64.SetValue(assemblyName.ToLower().Replace(".exe", ".vshost.exe"), browser_value, Microsoft.Win32.RegistryValueKind.DWord);
                        }
                    }
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// Registers the app to use not the quirks mode in browser which would let the auth window fail
        /// See http://www.west-wind.com/weblog/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version for more details
        /// If this registry key is not set the authorization window will fail
        /// </summary>
        /// <param name="whichBrowserToUse">Which type of IE to use (should be at least IE 8 always)</param>
        /// <param name="assemblyName">Name of the .exe (leave empty for autodetect)</param>
        /// <param name="alsoCreateVshostEntry">Will create a second entry with assembly.vshost.exe instead of assembly.exe as this procress is the debugging process in Visual Studio</param>
        public static bool registerAppInRegistry(registerBrowserEmulationValue whichBrowserToUse, string assemblyName = "", bool alsoCreateVshostEntry = true)
        {
            if (whichBrowserToUse == registerBrowserEmulationValue.NoChange)
            {
                return true;
            }
            Microsoft.Win32.RegistryKey registryKey = null;
            Microsoft.Win32.RegistryKey registryKey64 = null;
            if (string.IsNullOrEmpty(assemblyName))
            {
                assemblyName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
            }

            registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION");
            if (Environment.Is64BitOperatingSystem)
            {
                registryKey64 = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION");
            }
            if (whichBrowserToUse == registerBrowserEmulationValue.Remove)
            {
                try
                {
                    registryKey.DeleteValue(assemblyName);
                    if (registryKey64 != null)
                    {
                        registryKey64.DeleteValue(assemblyName);
                        return true;
                    }
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                int browser_value = 0;
                // browser registry values taken from http://msdn.microsoft.com/en-us/library/ee330730%28v=vs.85%29.aspx#browser_emulation
                switch (whichBrowserToUse)
                {
                    case registerBrowserEmulationValue.IE7IfDoctypeAvailable:
                        browser_value = 7000;
                        break;
                    case registerBrowserEmulationValue.IE8IfDoctypeAvailable:
                        browser_value = 8000;
                        break;
                    case registerBrowserEmulationValue.IE8Always:
                        browser_value = 8888;
                        break;
                    case registerBrowserEmulationValue.IE9IfDoctypeAvailable:
                        browser_value = 9000;
                        break;
                    case registerBrowserEmulationValue.IE9Always:
                        browser_value = 9999;
                        break;
                    case registerBrowserEmulationValue.IE10IfDoctypeAvailable:
                        browser_value = 10000;
                        break;
                    case registerBrowserEmulationValue.IE10Always:
                        browser_value = 10001;
                        break;
                    default:
                        browser_value = 0;
                        break;
                }
                try
                {
                    registryKey.SetValue(assemblyName, browser_value, Microsoft.Win32.RegistryValueKind.DWord);
                    if (alsoCreateVshostEntry)
                    {
                        registryKey.SetValue(assemblyName.ToLower().Replace(".exe", ".vshost.exe"), browser_value, Microsoft.Win32.RegistryValueKind.DWord);
                    }
                    if (registryKey64 != null)
                    {
                        registryKey64.SetValue(assemblyName, browser_value, Microsoft.Win32.RegistryValueKind.DWord);
                        if (alsoCreateVshostEntry)
                        {
                            registryKey64.SetValue(assemblyName.ToLower().Replace(".exe", ".vshost.exe"), browser_value, Microsoft.Win32.RegistryValueKind.DWord);
                        }
                    }
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                    return false;
                }

            }
            return true;
        }