前段时间在测试我们触摸屏联网性能的时候,由于屏的Mac地址通过注册表设置,而PB定制WinCE内核的时候就有一个默认值,所以一批下来Mac地址都一样,两台以上的机器一联网准出问题,通过注册表软件去修改Mac地址太麻烦了,所以做了一个小的Mac地址软件。
以前用VB做了一个注册表操作类(参见我的blog:http://blog.csdn.net/yefanqiu/archive/2004/07/13/40379.aspx ),功能比较详尽;现在用C#做了一个注册表操作类也别简单的只做数据读写了,索性也做个全功能的了。由于.Net在域名空间Microsoft.Win32中提供RegistryKey类,操作注册表还是比较方便的,唯感不足的时在操作多级子键的时候不很便捷,所以在封装注册表操作类的时候也把这部分内容给考虑进去了。
注册表操作类代码(可以用在.Net精简框架集下)如下:
///<summary>
///注册表操作类 -- 叶帆工作室
///</summary>
public class YFReg
{
public enum HKEY { HKEY_LOCAL_MACHINE = 0, HKEY_CLASSES_ROOT = 1, HKEY_CURRENT_USER = 2, HKEY_USERS = 3 };
private RegistryKey[] reg = new RegistryKey[4];
public YFReg()
{
reg[(int)HKEY.HKEY_LOCAL_MACHINE] = Registry.LocalMachine;
reg[(int)HKEY.HKEY_CLASSES_ROOT] = Registry.ClassesRoot;
reg[(int)HKEY.HKEY_CURRENT_USER] = Registry.CurrentUser;
reg[(int)HKEY.HKEY_USERS] = Registry.Users;
}
//读指定变量值
public string ReadValue(HKEY Root,string SubKey,string ValueName)
{
RegistryKey subKey=reg[(int)Root];
if (ValueName.Length == 0) return "[ERROR]";
try
{
if (SubKey.Length > 0)
{
string[] strSubKey = SubKey.Split('//');
foreach (string strKeyName in strSubKey)
{
subKey = subKey.OpenSubKey(strKeyName);
}
}
string strKey = subKey.GetValue(ValueName).ToString();
subKey.Close();
return strKey;
}
catch
{
return "[ERROR]";
}
}
//读指定变量的类型
public RegistryValueKind ReadValueType(HKEY Root, string SubKey, string ValueName)
{
RegistryKey subKey = reg[(int)Root];
if (ValueName.Length == 0) return RegistryValueKind.Unknown;
try
{
if (SubKey.Length > 0)
{
string[] strSubKey = SubKey.Split('//');
foreach (string strKeyName in strSubKey)
{
subKey = subKey.OpenSubKey(strKeyName);
}
}
RegistryValueKind valueType = subKey.GetValueKind(ValueName);
subKey.Close();
return valueType;
}
catch
{
return RegistryValueKind.Unknown;
}
}
//写指定变量值
public int WriteValue(HKEY Root, string SubKey, string ValueName, string ValueData)
{
return WriteValue(Root, SubKey, ValueName, ValueData, RegistryValueKind.String);
}
//写指定变量值
public int WriteValue(HKEY Root, string SubKey, string ValueName,object ValueData,RegistryValueKind ValueType)
{
RegistryKey subKey = reg[(int)Root];
if (ValueName.Length == 0) return 2;
try
{
if (SubKey.Length > 0)
{
string[] strSubKey = SubKey.Split('//');
foreach (string strKeyName in strSubKey)
{
subKey = subKey.CreateSubKey(strKeyName);
}
}
subKey.SetValue(ValueName, ValueData, ValueType);
subKey.Close();
return 0;
}
catch
{
return 1;
}
}
//删除指定变量
public int DeleteValue(HKEY Root, string SubKey, string ValueName)
{
RegistryKey subKey = reg[(int)Root];
try
{
if (SubKey.Length > 0)
{
string[] strSubKey = SubKey.Split('//');
foreach (string strKeyName in strSubKey)
{
subKey = subKey.OpenSubKey(strKeyName,true);
}
}
subKey.DeleteValue(ValueName, true);
subKey.Close();
return 0;
}
catch
{
return 1;
}
}
//创建指定的键
public int CreateKey(HKEY Root, string SubKey, string KeyName)
{
RegistryKey subKey = reg[(int)Root];
if (KeyName.Length == 0) return 2;
try
{
if (SubKey.Length > 0)
{
string[] strSubKey = SubKey.Split('//');
foreach (string strKeyName in strSubKey)
{
subKey = subKey.CreateSubKey(strKeyName);
}
}
subKey.CreateSubKey(KeyName);
subKey.Close();
return 0;
}
catch
{
return 1;
}
}
//删除指定的键
public int DeleteKey(HKEY Root, string SubKey, string KeyName)
{
RegistryKey subKey = reg[(int)Root];
try
{
if (SubKey.Length > 0)
{
string[] strSubKey = SubKey.Split('//');
foreach (string strKeyName in strSubKey)
{
subKey = subKey.OpenSubKey(strKeyName, true);
}
}
subKey.DeleteSubKeyTree(KeyName);
subKey.Close();
return 0;
}
catch
{
return 1;
}
}
//判断指定的键是否存在
public int IsExistKey(HKEY Root, string SubKey, string KeyName)
{
RegistryKey subKey = reg[(int)Root];
try
{
if (SubKey.Length > 0)
{
string[] strSubKey = SubKey.Split('//');
foreach (string strKeyName in strSubKey)
{
subKey = subKey.OpenSubKey(strKeyName);
}
}
string[] strSubKey1 = subKey.GetSubKeyNames();
foreach (string strKeyName in strSubKey1)
{
if (strKeyName == KeyName) return 0;
}
return 1;
}
catch
{
return 2;
}
}
//枚举指定的键的子键
public string[] EnumKeyName(HKEY Root, string SubKey)
{
RegistryKey subKey = reg[(int)Root];
if (SubKey.Length == 0) return null;
try
{
string[] strSubKey = SubKey.Split('//');
foreach (string strKeyName in strSubKey)
{
subKey = subKey.OpenSubKey(strKeyName);
}
string[] strKey = subKey.GetSubKeyNames();
subKey.Close();
return strKey;
}
catch
{
return null;
}
}
//枚举指定的键的值
public string[] EnumValueName(HKEY Root, string SubKey)
{
RegistryKey subKey = reg[(int)Root];
if (SubKey.Length == 0) return null;
try
{
string[] strSubKey = SubKey.Split('//');
foreach (string strKeyName in strSubKey)
{
subKey = subKey.OpenSubKey(strKeyName);
}
string[] strValue = subKey.GetValueNames();
subKey.Close();
return strValue;
}
catch
{
return null;
}
}
}
类出来了,用它读写Mac地址就是小菜一碟了,下面是读写Mac地址的示例代码:
private void btnRead_Click(object sender, EventArgs e)
{
txtMAC1.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress0");
txtMAC2.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress1");
txtMAC3.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress2");
txtMAC4.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress3");
txtMAC5.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress4");
txtMAC6.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress5");
}
private void btnWrite_Click(object sender, EventArgs e)
{
int intRet = 0;
intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress0", txtMAC1.Text);
intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress1", txtMAC2.Text);
intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress2", txtMAC3.Text);
intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress3", txtMAC4.Text);
intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress4", txtMAC5.Text);
intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm/DM9CE1/Parms", "SoftwareMacAddress5", txtMAC6.Text);
if (intRet > 0)
{
MessageBox.Show("更改失败");
}
}