C#通过修改注册表改变IE默认选项
生活随笔
收集整理的這篇文章主要介紹了
C#通过修改注册表改变IE默认选项
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
修改注冊表,這個代碼好實現(xiàn),關(guān)鍵是怎么找到對應(yīng)的注冊表值,也就是說畫一條線很容易,難的是找到要在哪里畫,然后我百度了一圈,出來的都是畫線的,沒有指出或者指出的不全的注冊表對應(yīng)值,只能FQ谷歌了,也就有了今天這兩步。
第一步:找到要設(shè)置的選項:注冊表與IE設(shè)置選項對應(yīng)表
第二步:根據(jù)下面code修改
IE的選項包括Activex插件相關(guān),還有設(shè)置相關(guān),我們項目上用到了一個ActiveX插件,但是不能讓用戶來設(shè)置復(fù)雜的插件設(shè)置,我們可以在安裝包里添加相關(guān)的選項修改信息。
修改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){//獲取用戶設(shè)定的安裝目標路徑, 注意,需要在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 ") + "開始添加環(huán)境變量: " + installPath);//處理環(huán)境變量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 對標記為可安全執(zhí)行腳本的 ActiveX 控件執(zhí)行腳本 //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 ") + "開始啟用:對標記為可安全執(zhí)行腳本的 ActiveX 控件執(zhí)行腳本");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();//設(shè)置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){//獲取用戶設(shè)定的安裝目標路徑, 注意,需要在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 ") + "開始卸載");//刪除環(huán)境變量string pathlist, pathAfter = "";Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "開始刪除環(huán)境變量" + 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);//清理注冊表 }} }?
轉(zhuǎn)載于:https://www.cnblogs.com/ningheshutong/p/8133832.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的C#通过修改注册表改变IE默认选项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【iCore3 双核心板】例程三十五:H
- 下一篇: c# char unsigned_dll