none
怎样使用C#操作WINCE的注册表? RRS feed

  • 问题

  • 我想使用C#操作WINCE的注册表,可是不支持WIN32命名空间,请问用什么方法来操作?可以给个例子吗?
    2008年3月13日 9:45

答案

  • 可以支持WIN32命名空间,这是我做的WinCE平台下的操作注册表的程序。你可以参考一下

     

    Code Snippet

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Win32;

    namespace MacAddr
    {
        public partial class frmMain : Form
        {
            YFReg reg = new YFReg();
            public frmMain()
            {
                InitializeComponent();          
            }

            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",int.Parse(txtMAC1.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress1", int.Parse(txtMAC2.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress2", int.Parse(txtMAC3.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress3", int.Parse(txtMAC4.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress4", int.Parse(txtMAC5.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress5", int.Parse(txtMAC6.Text), RegistryValueKind.DWord);
                if (intRet > 0)
                {
                    MessageBox.Show("更改失败");
                }
            }

            private void txtMAC1_GotFocus(object sender, EventArgs e)
            {
                inputPanel1.Enabled = true;
            }

            private void txtMAC1_LostFocus(object sender, EventArgs e)
            {
                inputPanel1.Enabled = false;
            }       
        }

        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;
                }
            }
        }
    }

     

     

    2008年3月14日 2:18
    版主

全部回复

  • 可以支持WIN32命名空间,这是我做的WinCE平台下的操作注册表的程序。你可以参考一下

     

    Code Snippet

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Win32;

    namespace MacAddr
    {
        public partial class frmMain : Form
        {
            YFReg reg = new YFReg();
            public frmMain()
            {
                InitializeComponent();          
            }

            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",int.Parse(txtMAC1.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress1", int.Parse(txtMAC2.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress2", int.Parse(txtMAC3.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress3", int.Parse(txtMAC4.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress4", int.Parse(txtMAC5.Text), RegistryValueKind.DWord);
                intRet += reg.WriteValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress5", int.Parse(txtMAC6.Text), RegistryValueKind.DWord);
                if (intRet > 0)
                {
                    MessageBox.Show("更改失败");
                }
            }

            private void txtMAC1_GotFocus(object sender, EventArgs e)
            {
                inputPanel1.Enabled = true;
            }

            private void txtMAC1_LostFocus(object sender, EventArgs e)
            {
                inputPanel1.Enabled = false;
            }       
        }

        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;
                }
            }
        }
    }

     

     

    2008年3月14日 2:18
    版主
  • Great encapsulation for that! OpenNETCF.org also have a similar solution

    Regards~

    2008年3月14日 5:00
    版主
  • 谢谢,真是太感谢你了

    2008年3月19日 2:39