最近在项目中遇到用C#调用注册表,修改IE选项高级选项卡中的“显示图片”复选框,用到的代码如下:
RegisterHelper.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32; //添加针对操作注册表的应用
</pre><pre name="code" class="csharp"> public class RegisterHelper
{
/// <summary>
/// 读取指定名称的注册表的值
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string GetRegistryData(RegistryKey root, string subKey, string name)
{
string registData = string.Empty;
RegistryKey myKey = root.OpenSubKey(subKey, true);
if (myKey != null)
{
registData = myKey.GetValue(name).ToString();
}
return registData;
}
/// <summary>
/// 向注册表中写数据
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="keyName"></param>
/// <param name="keyValue"></param>
public static void SetRegistryData(RegistryKey root, string subKey, string keyName, string keyValue)
{
RegistryKey aimDir = root.CreateSubKey(subKey);
aimDir.SetValue(keyName, keyValue);
}
/// <summary>
/// 删除注册表中指定的注册项
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="keyName"></param>
public static void DeleteRegist(RegistryKey root, string subKey, string keyName)
{
string[] subkeyNames;
RegistryKey myKey = root.OpenSubKey(subKey, true);
subkeyNames = myKey.GetSubKeyNames();
foreach (string aimKey in subkeyNames)
{
if (aimKey == keyName)
myKey.DeleteSubKeyTree(keyName);
}
}
/// <summary>
/// 判断指定注册表项是否存在
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="keyName"></param>
/// <returns></returns>
public static bool IsRegistryExits(RegistryKey root, string subKey, string keyName)
{
bool result = false;
string[] subKeyNames;
RegistryKey myKey = root.OpenSubKey(subKey, true);
subKeyNames = myKey.GetValueNames();
foreach (string name in subKeyNames)
{
if (name == keyName)
{
result = true;
return result;
}
}
return result;
}
}
调用方式:
需要引用以下命名空间
using System.Security.Principal; //用户获取Windows SID 引用
using Microsoft.Win32;
//获取当前Windows用户
WindowsIdentity curUser = WindowsIdentity.GetCurrent();
//用户SID
SecurityIdentifier sid = curUser.User;
//用户全称
NTAccount ntacc = (NTAccount)sid.Translate(typeof(NTAccount));
Console.WriteLine("Windows SID:" + sid.Value);
Console.WriteLine("用户全称:" + ntacc.Value);
//IE选项中的“图片显示”操作在系统注册表中的路径为:HKEY_USERS\S-1-5-21-2145109174-1241497065-710993060-1001\Software\Microsoft\Internet Explorer\Main
//这个注册表下这个键值“Display Inline Images”,设置为yes或no,其中“S-1-5-21-2145109174-1241497065-710993060-1001”为Windows SID信息,在客户端使用的时候
//需要先获取(每个机器会不同)
//读注册表:
//RegistryHelper rh = new RegistryHelper();
//string portName = rh.GetRegistryData(Registry.LocalMachine, "SOFTWARE\\TagReceiver\\Params\\SerialPort", "PortName");
//写注册表:
//RegistryHelper rh = new RegistryHelper();
//rh.SetRegistryData(Registry.LocalMachine, "SOFTWARE\\TagReceiver\\Params\\SerialPort", "PortName", portName);
//读取Display Inline Images
string displayImgPath = sid.Value + @"\Software\Microsoft\Internet Explorer\Main";
string displayImgValue = RegisterHelper.GetRegistryData(Registry.Users, displayImgPath, "Display Inline Images");
//IE选择中的“显示图片”勾选
if (displayImgValue == "yes")
{
//改变yes为no,将IE选项中的“显示图片”设置为不可用
RegisterHelper.SetRegistryData(Registry.Users, displayImgPath, "Display Inline Images", "no");
//重新打开浏览器
}
Console.ReadKey();