怎么读写注册表的?谁能给我个实例的哟~~~先谢谢了~~~在线等待~~

解决方案 »

  1.   

    http://longhorn.msdn.microsoft.com/lhsdk/ref/ns/microsoft.win32/c/registrykey/m/setvalue.aspx
    http://community.csdn.net/Expert/topic/2894/2894881.xml?temp=.7155573
      

  2.   

    /// 读取注册表键值
    /// </summary>
    public static string ReadRegistryKey(string name)
    {
    Microsoft.Win32.RegistryKey reg = Microsoft.Win32.Registry.CurrentUser;
    string path="Software\\" + Application.ProductName; if (reg.OpenSubKey(path) == null)
    reg.CreateSubKey(path); reg = reg.OpenSubKey(path, true); string str;
    if (reg.GetValue(name) == null)
    str="";
    else
        str = reg.GetValue(name).ToString(); reg.Close();
    return str;
    }/// <summary>
    /// 设置注册表键值
    /// </summary>
    public static void SetRegistryKey(string name, string Value)
    {
    Microsoft.Win32.RegistryKey reg = Microsoft.Win32.Registry.CurrentUser;
    string path="Software\\" + Application.ProductName; if (reg.OpenSubKey(path) == null)
    reg.CreateSubKey(path); reg=reg.OpenSubKey(path,true); reg.SetValue(name, Value);
    reg.Close();
    }好用就把分都给我吧
      

  3.   

    using System;
    using Microsoft.Win32;public class Example
    {
        public static void Main()
        {
            // Delete and recreate the test key.
            Registry.CurrentUser.DeleteSubKey("RegistrySetValueExample", false);
            RegistryKey rk = Registry.CurrentUser.CreateSubKey("RegistrySetValueExample");        // Create name/value pairs.        // Numeric values that cannot be interpreted as DWord (int) values
            // are stored as strings.
            rk.SetValue("LargeNumberValue1", (long) 42);
            rk.SetValue("LargeNumberValue2", 42000000000);        rk.SetValue("DWordValue", 42);
            rk.SetValue("MultipleStringValue", new string[] {"One", "Two", "Three"});
            rk.SetValue("BinaryValue", new byte[] {10, 43, 44, 45, 14, 255});        // This overload of SetValue does not support expanding strings. Use
            // the overload that allows you to specify RegistryValueKind.
            rk.SetValue("StringValue", "The path is %PATH%");
            // Display all name/value pairs stored in the test key, with each
            // registry data type in parentheses.
            //
            string[] valueNames = rk.GetValueNames();
            foreach (string s in valueNames)
            {
                RegistryValueKind rvk = rk.GetValueKind(s);
                switch (rvk)
                {
                    case RegistryValueKind.MultiString :
                        string[] values = (string[]) rk.GetValue(s);
                        Console.Write("\r\n {0} ({1}) = \"{2}\"", s, rvk, values[0]);
                        for (int i = 1; i < values.Length; i++)
                        {
                            Console.Write(", \"{0}\"", values[i]);
                        }
                        Console.WriteLine();
                        break;
                    
                    case RegistryValueKind.Binary :
                        byte[] bytes = (byte[]) rk.GetValue(s);
                        Console.Write("\r\n {0} ({1}) = {2:X2}", s, rvk, bytes[0]);
                        for (int i = 1; i < bytes.Length; i++)
                        {
                            // Display each byte as two hexadecimal digits.
                            Console.Write(" {0:X2}", bytes[i]);
                        }
                        Console.WriteLine();
                        break;
                    
                    default :
                        Console.WriteLine("\r\n {0} ({1}) = {2}", s, rvk, rk.GetValue(s));
                        break;
                }
            }
        }
    }
      

  4.   

    //写注册表
    RegistryKey regWrite;
    //往HKEY_CURRENT_USER主键里的Software子键下写一个名为“Test”的子键
    //如果Test子键已经存在系统会自动覆盖它
    regWrite = Registry.CurrentUser.CreateSubKey("Software\\Test");
    //往Test子键里添两条数据项,一条名为"Name",另一条名为"Sex"
    //值分别是"luolie","男"
    regWrite.SetValue("Name","luolie");
    regWrite.SetValue("Sex","男");
    //关闭该对象
    regWrite.Close();
    读注册表
    RegistryKey regRead;
    //读取HKEY_CURRENT_USER主键里的Software子键下名为“Test”的子键
    regRead= Registry.CurrentUser.OpenSubKey("Software\\Test",true);
    if(regRead==null) //如果该子键不存在
    {
    MessageBox.Show("No Data!");
    }
    else

             object obj= regRead.GetValue("Name");  //读取“Name”项的值
             textBox1.Text = obj.ToString();       //显示在TextBox里

    //关闭该对象
    oReg.Close();