C#通过修改注册表改变IE默认选项
修改注册表,这个代码好实现,关键是怎么找到对应的注册表值,也就是说画一条线很容易,难的是找到要在哪里画,然后我百度了一圈,出来的都是画线的,没有指出或者指出的不全的注册表对应值,只能FQ谷歌了,也就有了今天这两步。
第一步:找到要设置的选项:注册表与IE设置选项对应表
第二步:根据下面code修改
IE的选项包括Activex插件相关,还有设置相关,我们项目上用到了一个ActiveX插件,但是不能让用户来设置复杂的插件设置,我们可以在安装包里添加相关的选项修改信息。
修改code如下:
using Microsoft.Win32; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using System.Runtime.InteropServices; namespace ExtInstaller { [RunInstaller(true)] public partial class OCXInstaller : System.Configuration.Install.Installer { public OCXInstaller() { InitializeComponent(); Trace.Listeners.Clear(); //自动清空缓冲(即时写入) Trace.AutoFlush = true; this.AfterInstall += new InstallEventHandler(OCXInstaller_AfterInstall); this.BeforeUninstall += new InstallEventHandler(OCXInstaller_BeforeUninstall); } private void OCXInstaller_AfterInstall(object sender, InstallEventArgs e) { //获取用户设定的安装目标路径, 注意,需要在Setup项目里面自定义操作的属性栏里面的CustomActionData添加上/targetdir="[TARGETDIR]\" string installPath = this.Context.Parameters["targetdir"]; installPath = installPath.TrimEnd('\\') + "\\"; Trace.Listeners.Add(new TextWriterTraceListener(installPath + "UnisOCX.log")); Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始安装"); Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始添加环境变量: " + installPath); //处理环境变量 string pathlist; bool isPathExist = false; pathlist = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine); string[] list = pathlist.Split(';'); foreach (string item in list) { if (item == installPath) isPathExist = true; } if (!isPathExist) { Environment.SetEnvironmentVariable("PATH", pathlist + ";" + installPath, EnvironmentVariableTarget.Machine); } //添加信任站点 string strURL = "127.0.0.1"; RegistryKey hkml = Registry.CurrentUser;//读取HKEY_CURRENT_USER string address = @"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\INTERNET SETTINGS\ZONEMAP\RANGES"; RegistryKey key1 = hkml.OpenSubKey(address, true); strURL = this.Context.Parameters["url1"]; RegistryKey Name1 = key1.CreateSubKey("Url1");//新建项 //Name1可随便改 Name1.SetValue(":Range", strURL, RegistryValueKind.String);//赋值 218.66.55.77按需求修改 Name1.SetValue("http", 0x2, RegistryValueKind.DWord);//赋值 Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始添加可信站点1:" + strURL); strURL = this.Context.Parameters["url2"]; RegistryKey Name2 = key1.CreateSubKey("Url2");//新建项 //Name1可随便改 Name2.SetValue(":Range", strURL, RegistryValueKind.String);//赋值 218.66.55.77按需求修改 Name2.SetValue("http", 0x2, RegistryValueKind.DWord);//赋值 Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始添加可信站点2:" + strURL); strURL = this.Context.Parameters["url3"]; RegistryKey Name3 = key1.CreateSubKey("Url3");//新建项 //Name1可随便改 Name3.SetValue(":Range", strURL, RegistryValueKind.String);//赋值 218.66.55.77按需求修改 Name3.SetValue("http", 0x2, RegistryValueKind.DWord);//赋值 Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始添加可信站点3:" + strURL); strURL = this.Context.Parameters["url4"]; RegistryKey Name4 = key1.CreateSubKey("Url4");//新建项 //Name1可随便改 Name4.SetValue(":Range", strURL, RegistryValueKind.String);//赋值 218.66.55.77按需求修改 Name4.SetValue("http", 0x2, RegistryValueKind.DWord);//赋值 Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始添加可信站点4:" + strURL); key1.Flush(); key1.Close(); //修改IE的安全性 //1001 下载已签名的 ActiveX 控件 //1004 下载未签名的 ActiveX 控件 //1200 运行 ActiveX 控件和插件 //1201 对没有标记为安全的 ActiveX 控件进行初始化和脚本运行 //1405 对标记为可安全执行脚本的 ActiveX 控件执行脚本 //2201 ActiveX 控件自动提示 ** Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始启用:下载已签名的 ActiveX 控件"); Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始启用:下载未签名的 ActiveX 控件"); Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始启用:运行 ActiveX 控件和插件"); Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始启用:对没有标记为安全的 ActiveX 控件进行初始化和脚本运行"); Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始启用:对标记为可安全执行脚本的 ActiveX 控件执行脚本"); Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始启用:ActiveX 控件自动提示"); address = @"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\INTERNET SETTINGS\Zones"; RegistryKey key2 = hkml.OpenSubKey(address, true); RegistryKey respect = key2.OpenSubKey("2", true); respect.SetValue("1001", 0);// 3=禁用、0=启用、1=提示 respect.SetValue("1004", 0);// 3=禁用、0=启用、1=提示 respect.SetValue("1200", 0);// 3=禁用、0=启用、1=提示 respect.SetValue("1201", 0);// 3=禁用、0=启用、1=提示 respect.SetValue("1405", 0);// 3=禁用、0=启用、1=提示 respect.SetValue("2201", 0);// 3=禁用、0=启用、1=提示 key2.Flush(); key2.Close(); //设置IE退出时删除历史记录,1表示退出时删除,0表示退出时不删除 address = @"SOFTWARE\MICROSOFT\INTERNET EXPLORER\PRIVACY"; RegistryKey key3 = hkml.OpenSubKey(address, true); key3.SetValue("ClearBrowsingHistoryOnExit", "1"); key3.Flush(); key3.Close(); } //卸载程序后处理 private void OCXInstaller_BeforeUninstall(object sender, InstallEventArgs e) { //获取用户设定的安装目标路径, 注意,需要在Setup项目里面自定义操作的属性栏里面的CustomActionData添加上/targetdir="[TARGETDIR]\" string installPath = this.Context.Parameters["targetdir"]; installPath = installPath.TrimEnd('\\') + "\\"; Trace.Listeners.Add(new TextWriterTraceListener(installPath + "UnisOCX.log")); Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始卸载"); //删除环境变量 string pathlist, pathAfter = ""; Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "开始删除环境变量" + installPath); pathlist = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine); string[] list = pathlist.Split(';'); foreach (string item in list) { if ((item != installPath) && (item.Trim() != "")) pathAfter += item + ";"; } pathAfter.Trim('\\'); Environment.SetEnvironmentVariable("PATH", pathAfter, EnvironmentVariableTarget.Machine); //清理注册表 } } }