C#实现手动在IE中增加受信任/受限制站点(更新过后的代码)
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Win32;
using System.Collections;
public partial class _AddFiduciary : System.Web.UI.Page
{
#region define
string EMPTYSTRING = "";
string _txtFidStr = "";
string _folderName = "";
string _subFolderName = "";
string _combStr = "";
int _dwordValue = 0;
int judgeGetSubKeyNamesLength = 0;
public const string REGISTRYADDRESS = "SOFTWARE""MICROSOFT""WINDOWS""CURRENTVERSION""INTERNET SETTINGS""ZONEMAP""DOMAINS";
//受信任的站点
public const string TRUST_NAME = "受信任站点";
public const int TRUST_DWORD_VALUE = 2;
//受限制的站点
public const string LIMIT_NAME = "受限制站点";
public const int LIMIT_DWORD_VALUE = 4;
public ArrayList _registData = new ArrayList();
public ArrayList _registLimitData = new ArrayList();
public const string HTTP = "http";
public const string HTTPS = "https";
public const string TRUST = "trust";
public const string LIMIT = "limit";
#endregion
#region 初始化
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
//清空ListBox
clearListBox();
dispAdd();
}
catch { }
}
}
#endregion
#region 增加
protected void btnAddress_Click(object sender, EventArgs e)
{
//取得文本栏中的值
if (!EMPTYSTRING.Equals(txtFiduciary.Text))
{
txtFidStr = txtFiduciary.Text;
//取得前缀
folderName = divPreStr(txtFidStr);
//取得后缀
subFolderName = divPostStr(txtFidStr);
//向注册表中写数据
writeToRegedit(folderName, subFolderName, dbListHttp.SelectedItem.Text, writeDwordValue());
//清空ListBox
clearListBox();
dispAdd();
txtFiduciary.Text = EMPTYSTRING;
}
}
#endregion
#region 删除
protected void delAddress_Click(object sender, EventArgs e)
{
if (lstLimitList.SelectedItem != null || lboxDispFidAdd.SelectedItem != null)
{
if (null != (lboxDispFidAdd.SelectedItem))
{
DeleteRegist(lboxDispFidAdd.SelectedItem.Text);
}
else
{
DeleteRegist(lstLimitList.SelectedItem.Text);
}
}
}
#endregion
#region 判断站点类型
private string writeDwordValue()
{
if (LIMIT_NAME.Equals(ddlWeb.SelectedItem.Text))
{
return LIMIT_DWORD_VALUE.ToString();
}
else
{
return TRUST_DWORD_VALUE.ToString();
}
}
#endregion
#region 显示受信任网站
private void dispAdd()
{
ArrayList getRegData = readRegistryData();
for (int i = 0; i < getRegData.Count; i = i + 2)
{
if ((i + 1) <= (getRegData.Count - 1))
{
if (!EMPTYSTRING.Equals(getRegData[i + 1].ToString()))
{
_combStr = getRegData[i + 1].ToString() + "." + getRegData[i].ToString();
if (TRUST.Equals(divBlowStr(getRegData[i].ToString())))
{
lboxDispFidAdd.Items.Add(divPreBlowStr(_combStr));
}
else
{
lstLimitList.Items.Add(divPreBlowStr(_combStr));
}
}
}
}
}
#endregion
#region 删除注册项
/**/
/// <summary>
/// 删除注册项
/// </summary>
/// <param name="listName">传入下拉框中选中的值</param>
private void DeleteRegist(string listName)
{
RegistryKey hkml = Registry.CurrentUser;
RegistryKey software = hkml.OpenSubKey(REGISTRYADDRESS, true);
//取得后缀
folderName = divPostStr(listName);
//取得前缀
subFolderName = divPreStr(listName);
//删除
software.DeleteSubKeyTree(folderName);
//清空ListBox
clearListBox();
dispAdd();
}
#endregion
#region 向注册表中读数据
/**/
/// <summary>
/// 向注册表中读数据
/// </summary>
/// <returns></returns>
private ArrayList readRegistryData()
{
RegistryKey hkml = Registry.CurrentUser;
RegistryKey domains = hkml.OpenSubKey(REGISTRYADDRESS, true);
string[] folderNames = domains.GetSubKeyNames();
foreach (string folderName in folderNames)
{
//registData.Add(folderName);
RegistryKey subFolderName = domains.OpenSubKey(folderName, true);
string[] subfNames = subFolderName.GetSubKeyNames();
if (0 != subfNames.Length)
{
foreach (string subfName in subfNames)
{
//registData.Add(subfName);
RegistryKey thriceFolderName = subFolderName.OpenSubKey(subfName, true);
//判断dwordValue是否等于2或者4
JudegeDwordValue(domains, thriceFolderName, folderName, subfName);
}
}
//其他情况,删除此项
else
{
//删除注册表中的项
domains.DeleteSubKeyTree(folderName);
//删除ArrayList中的项
delToArrayList(folderName, EMPTYSTRING);
//清空ListBox
clearListBox();
}
}
return registData;
}
#endregion
#region 判断dwordValue是否等于2或者4或者其他情况
private void JudegeDwordValue(RegistryKey domains, RegistryKey thriceFolderName, string folderName, string subfName)
{
//受信任站点
//http
if (null != thriceFolderName.GetValue(HTTP) && TRUST_DWORD_VALUE.ToString() == thriceFolderName.GetValue(HTTP).ToString())
{
dwordValue = System.Convert.ToInt32(thriceFolderName.GetValue(HTTP));
addToArrayList(folderName, subfName, TRUST);
}
//https
else if (null != thriceFolderName.GetValue(HTTPS) && TRUST_DWORD_VALUE.ToString() == thriceFolderName.GetValue(HTTPS).ToString())
{
dwordValue = System.Convert.ToInt32(thriceFolderName.GetValue(HTTPS));
addToArrayList(folderName, subfName, TRUST);
}
//受限制站点
//http
else if (null != thriceFolderName.GetValue(HTTP) && LIMIT_DWORD_VALUE.ToString() == thriceFolderName.GetValue(HTTP).ToString())
{
dwordValue = System.Convert.ToInt32(thriceFolderName.GetValue(HTTP));
addToArrayList(folderName, subfName, LIMIT);
}
//https
else if (null != thriceFolderName.GetValue(HTTPS) && LIMIT_DWORD_VALUE.ToString() == thriceFolderName.GetValue(HTTPS).ToString())
{
dwordValue = System.Convert.ToInt32(thriceFolderName.GetValue(HTTPS));
addToArrayList(folderName, subfName, LIMIT);
}
//other
else
{
//删除注册表中的项
domains.DeleteSubKeyTree(folderName);
//删除ArrayList中的项
delToArrayList(folderName, subfName);
//清空ListBox
clearListBox();
}
}
#endregion
#region 向ArrayList中添加无素
private void addToArrayList(string folderName, string subfName, string flag)
{
if (TRUST.Equals(flag))
{
registData.Add(folderName + "_" + TRUST);
registData.Add(subfName);
}
else
{
registData.Add(folderName + "_" + LIMIT);
registData.Add(subfName);
}
}
#endregion
#region 删除ArrayList中的项
private void delToArrayList(string folderName, string subfName)
{
if (!EMPTYSTRING.Equals(subfName))
{
registData.Remove(subfName);
registData.Remove(folderName);
}
else
{
registData.Remove(folderName);
}
}
#endregion
#region 初始化ListBox
private void clearListBox()
{
lboxDispFidAdd.Items.Clear();
lstLimitList.Items.Clear();
}
#endregion
#region 向注册表中写数据
/**/
/// <summary>
/// 向注册表中写数据
/// </summary>
/// <param name="folderName">前缀</param>
/// <param name="subFolderName">后缀</param>
/// <param name="name">key</param>
/// <param name="tovalue">value</param>
private void writeToRegedit(string folderName, string subFolderName, string name, string value)
{
//HKEY_CURRENT_USER
RegistryKey hklm = Registry.CurrentUser;
//open folder
RegistryKey domains = hklm.OpenSubKey(REGISTRYADDRESS, true);
if (!IsRegeditExit(domains, subFolderName, folderName))
{
//create first folder
RegistryKey credir = domains.CreateSubKey(subFolderName);
//create second folder
RegistryKey creSubfolder = credir.CreateSubKey(folderName);
//AddNewObject http 2
creSubfolder.SetValue(name, value, RegistryValueKind.DWord);
}
}
#endregion
#region 分割点号前面的字符串
/**/
/// <summary>
/// 分割点号前面的字符串(divsional PreFix String)
/// </summary>
/// <param name="txtStr">传入文本栏中输入的值</param>
/// ruilei.cnblogs.com
/// <returns>ruilei</returns>
private string divPreStr(string txtPreStr)
{
string[] txtArray = txtPreStr.Split('.');
txtPreStr = txtArray[0].ToString();
return txtPreStr;
}
private string divPreBlowStr(string txtPreBlowStr)
{
string[] txtArray = txtPreBlowStr.Split('_');
txtPreBlowStr = txtArray[0].ToString();
return txtPreBlowStr;
}
#endregion
#region 分割点号后面字符串
/**/
/// <summary>
/// 分割点号后面字符串(divsional Postfix String)
/// </summary>
/// <param name="txtStr">传入文本栏中输入的值</param>
/// ruilei.cnblogs.com
/// <returns>cnblogs.com</returns>
private string divPostStr(string txtPostStr)
{
int pointPosStr = txtPostStr.IndexOf('.');
txtPostStr = txtPostStr.Substring(pointPosStr + 1).ToString();
return txtPostStr;
}
private string divBlowStr(string txtBlowStr)
{
int pointPosStr = txtBlowStr.IndexOf('_');
txtBlowStr = txtBlowStr.Substring(pointPosStr + 1).ToString();
return txtBlowStr;
}
#endregion
#region 判断是否有重复
private bool IsRegeditExit(RegistryKey domains, string subFolderName, string folderName)
{
bool _exit = false;
if (judgeGetSubKeyNamesLength != domains.GetSubKeyNames().Length)
{
string[] folderNames = domains.GetSubKeyNames();
RegistryKey credir = domains.OpenSubKey(subFolderName, true);
RegistryKey creSubfolder = credir.OpenSubKey(folderName, true);
if (null != creSubfolder.GetValue(HTTP) && TRUST_DWORD_VALUE.ToString().Equals(creSubfolder.GetValue(HTTP).ToString()))
{
_exit = true;
}
else if (null != creSubfolder.GetValue(HTTPS) && LIMIT_DWORD_VALUE.ToString().Equals(creSubfolder.GetValue(HTTPS).ToString()))
{
_exit = true;
}
}
return _exit;
}
#endregion
#region property
public string txtFidStr
{
set
{
_txtFidStr = value;
}
get
{
return _txtFidStr;
}
}
public string folderName
{
set
{
_folderName = value;
}
get
{
return _folderName;
}
}
public string subFolderName
{
set
{
_subFolderName = value;
}
get
{
return _subFolderName;
}
}
public ArrayList registData
{
set
{
_registData = value;
}
get
{
return _registData;
}
}
public string combStr
{
set
{
_combStr = value;
}
get
{
return _combStr;
}
}
public int dwordValue
{
set
{
_dwordValue = value;
}
get
{
return _dwordValue;
}
}
#endregion
}