Friday, 5 August 2016

Change IE settings programmatically in C#

Also Covers
Add trusted site in Internet Explorer in C#.
How to change Internet Explorer Settings like ActiveX enable disable in C#?
How to all Run Java in IE programmatically in C#?


Hi all, if you families with C# look at the below Helper Class IEOptions.cs and its implementation in program.cs.
It is a simple console application which help you to change IE settings.


IEOptions.cs Code
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Change_Internet_Explorer_Settings_In_CSharp
{
    class IEOptions
    {
        public class Security
        {
            public enum Zones
            {
                 My_Computer = 0,
                    Local_Intranet_Zone = 1,
                    Trusted_sites_Zone = 2,
                    Internet_Zone= 3,
                    Restricted_Sites_Zone= 4 
            }  
            

            public enum CustomLevelSettings
            {        
               ActiveX_plugins_Download_signed_ActiveX_controls= 1001,
               ActiveX_plugins_Download_unsigned_ActiveX_controls= 1004,
               ActiveX_plugins_Run_ActiveX_controls_and_plug_ins= 1200,
               ActiveX_plugins_Initialize_and_script_ActiveX_controls = 1201,
               Miscellaneous_Allow_scripting_of_Internet_Explorer_Web_browser_control= 1206,
               Reserved= 1207,
               ActiveX_plugins_Allow_previously_unused_ActiveX_controls_to_run_without_prompt= 1208,
               ActiveX_plugins_Allow_Scriptlets= 1209,
               ActiveX_controls_and_plug_ins_Script_ActiveX_controls_marked_as_safe_for_scripting= 1405,
               Scripting_Active_scripting= 1400,
               Scripting_Scripting_of_Java_applets= 1402,
               Miscellaneous_Access_data_sources_across_domains= 1406,
               Scripting_Allow_Programmatic_clipboard_access= 1407,
               Scripting_Enable_XSS_Filter= 1409,
               Miscellaneous_Submit_non_encrypted_form_data= 1601,
               Downloads_Font_download= 1604,
               Run_Java= 1605, 
               Miscellaneous_Userdata_persistence= 1606,
               Miscellaneous_Navigate_sub_frames_across_different_domains = 1607, 
               Miscellaneous_Allow_META_REFRESH= 1608,
               Miscellaneous_Display_mixed_content= 1609,
               Miscellaneous_Installation_of_desktop_items= 1800,
               Miscellaneous_Drag_and_drop_or_copy_and_paste_files= 1802,
               Downloads_File_Download= 1803
            }

            public enum Values
            {
                Enable = 0,
                Disable = -1,
                Prompt = 1,
            }

            public static string AddTrustedSites(string url, int zone)
            {
                string _resp = "";
                try
                {
                    Match match = Regex.Match(url, @"\A(.+)://((.+)\.)?([^.]+\.[^.]+)\Z");
                    string protocol = match.Groups[1].Value;
                    string subdomain = match.Groups[3].Value;
                    string domain = match.Groups[4].Value;
                    if (domain == "")
                    {
                        match = Regex.Match(url, @"\A(.+)://(.+)\Z");
                        protocol = match.Groups[1].Value;
                        subdomain = "";
                        domain = match.Groups[2].Value;
                    }
                    if (protocol == "" || domain == "")
                    {
                        throw new Exception("Invalid URL " + url + ". Url must start with http:// OR https://");
                    }

                    string key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\" + domain;
                    RegistryKey regKeyDomain = Registry.CurrentUser.CreateSubKey(key);

                    using (regKeyDomain)
                    {
                        if (subdomain == "")
                        {
                            regKeyDomain.SetValue(protocol, zone);
                        }
                        else
                        {
                            RegistryKey regKeySubdomain = regKeyDomain.CreateSubKey(subdomain);
                            using (regKeySubdomain)
                            {
                                regKeySubdomain.SetValue(protocol, zone);
                            }
                        }

                        _resp = "success:" + url + " added as trusted site.";
                    }
                }
                catch (Exception ex)
                {
                    _resp = "failed:" + ex.Message;
                }
                return _resp;

            }


            public static string SetCustomLevelSetting(int zone, int setting, int value)
            {
                string resp = "";
                try
                {
                    RegistryKey ChangeSettings = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\" + (int)zone, true);
                    if (ChangeSettings.GetValue(setting.ToString(), zone).ToString() != value.ToString())
                    {
                        ChangeSettings.SetValue(((int)setting).ToString(), value , RegistryValueKind.DWord);
                    }
                    ChangeSettings.Close();
                    resp = "success:" + ((CustomLevelSettings)setting).ToString().Replace("_"," ") + " : " + ((Values)value).ToString() ;
                }
                catch (Exception ex)
                {
                    resp = "failed:" + ex.Message;
                }
                return resp;
            }

   


        }


    }
}


Program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Change_Internet_Explorer_Settings_In_CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Capricorn CA 2014 Tool for IE settings";

            Console.WriteLine("\n\nProcessing...\n");
            string t1 = IEOptions.Security.AddTrustedSites("http://www.asptricks.net", (int)IEOptions.Security.Zones.Trusted_sites_Zone);
            
            string s1 = IEOptions.Security.SetCustomLevelSetting(
                (int)IEOptions.Security.Zones.Trusted_sites_Zone,
                (int)IEOptions.Security.CustomLevelSettings.ActiveX_plugins_Download_signed_ActiveX_controls,
                (int)IEOptions.Security.Values.Prompt
                );

            string s2 = IEOptions.Security.SetCustomLevelSetting(
                (int)IEOptions.Security.Zones.Trusted_sites_Zone,
                (int)IEOptions.Security.CustomLevelSettings.ActiveX_plugins_Initialize_and_script_ActiveX_controls,
                (int)IEOptions.Security.Values.Enable
                );

            string s3 = IEOptions.Security.SetCustomLevelSetting(
                (int)IEOptions.Security.Zones.Trusted_sites_Zone,
                (int)IEOptions.Security.CustomLevelSettings.ActiveX_plugins_Run_ActiveX_controls_and_plug_ins,
                (int)IEOptions.Security.Values.Enable
                );

            if (
                t1.StartsWith("success:") &&
                s1.StartsWith("success:") &&
                s2.StartsWith("success:") &&
                s3.StartsWith("success:")
                )
            {
                Console.WriteLine("All required setting configured. :-)\n");
                Console.WriteLine("Press [Enter] to exit..");
            }
            else {
                Console.WriteLine(t1);
                Console.WriteLine(s1);
                Console.WriteLine(s2);
                Console.WriteLine(s3);
            }
            Console.Read();
        }
    }
}