用C#操作类读写WinCE平台Mac地址

简介: 前段时间在测试我们触摸屏联网性能的时候,由于屏的Mac地址通过注册表设置,而PB定制WinCE内核的时候就有一个默认值,所以一批下来Mac地址都一样,两台以上的机器一联网准出问题,通过注册表软件去修改Mac地址太麻烦了,所以做了一个小的Mac地址软件。

前段时间在测试我们触摸屏联网性能的时候,由于屏的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("更改失败");
    }
}
相关文章
|
1月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
C#
58.c#:directory类
58.c#:directory类
12 0
|
1月前
|
C#
57.c#:directorylnfo类
57.c#:directorylnfo类
13 0
|
1月前
|
监控 C#
55.c#:file类
55.c#:file类
16 1
|
1月前
|
算法 C#
54.c#:random类
54.c#:random类
14 1
|
1月前
|
C#
51.c#:string类的静态方法
51.c#:string类的静态方法
20 1
|
1月前
|
C#
27.c#关键字sealed修饰类
27.c#关键字sealed修饰类
12 0
|
3月前
|
Java C#
C# 面向对象编程解析:优势、类和对象、类成员详解
OOP代表面向对象编程。 过程式编程涉及编写执行数据操作的过程或方法,而面向对象编程涉及创建包含数据和方法的对象。 面向对象编程相对于过程式编程具有几个优势: OOP执行速度更快,更容易执行 OOP为程序提供了清晰的结构 OOP有助于保持C#代码DRY("不要重复自己"),并使代码更易于维护、修改和调试 OOP使得能够创建完全可重用的应用程序,编写更少的代码并减少开发时间 提示:"不要重复自己"(DRY)原则是有关减少代码重复的原则。应该提取出应用程序中常见的代码,并将其放置在单一位置并重复使用,而不是重复编写。
51 0
|
1月前
|
C#
深入C#中的String类
深入C#中的String类
11 0
|
1月前
|
C#
C#学习系列相关之多线程(二)----Thread类介绍
C#学习系列相关之多线程(二)----Thread类介绍