在c#对注册表的操作中,如何写入二进制值和dword值,如何读取这两个值?可以的话请附源码!谢谢

解决方案 »

  1.   

    二进制没写过,DWORD,只要你在SetValue中的值是int,uint之类的,会自动写成DWORD.但取值时必须将制转化为int或uint
    不过在2.0以后,多了个 Microsoft.Win32.RegistryValueKind枚举,可以让你选择是不是按这种格式写注册表.
    我个人觉得,在1.1和1.0版中,写Object类型(实际是string或int的除外),应该是按Binary存储的,但Binary的内容可能是xml格式(这点是我通过.Net1.0或1.1写MSMQ联想到的,不过按.Net的习惯,估计应该是这样,也许猜得不对).因为我没试验过,不敢妄说,看看别人的回复吧.
      

  2.   

    这个很简单的,查一下MSDN吧,或者再这里搜索一下注册表
      

  3.   

    http://community.csdn.net/Expert/topic/5019/5019201.xml?temp=.5031702
      

  4.   

    写入
    ===================
    public static void SetValue(string registryPath,string valueName,RegistryValueKind keyType,object keyValue)
    {
    if(valueName == "@")
    valueName = String.Empty;
    if(keyValue.GetType() == typeof(string))
    {
    if(keyType == RegistryValueKind.ExpandString)
    {
    SystemType st = SystemTypeGetter.GetCurrentSystemType();
    if(st >= SystemType.WindowsXP)
    {
    RegCommandHelper.SetValue(registryPath,valueName,keyType,(string)keyValue);
    return;
    }
    else//REG_SZ处理
    {
    //key.SetValue(valueName,(string)keyValue);
    }
    }
    //return;
    }
    SetValue( registryPath, valueName, keyValue);
    }
    /// <summary>
    /// 设定指定键位键名的键值
    /// </summary>
    /// <param name="registryPath">要设置的项的名称或路径</param>
    /// <param name="valueName">要在其中存储数据的值的名称。</param>
    /// <param name="keyValue">要存储的数据。</param>
    private static void SetValue(string registryPath,string valueName,object keyValue)
    {
    RegistryKey key = GetRegistryKey( registryPath, true,true);
    if(valueName == "@")
    valueName = String.Empty;
    if(keyValue.GetType() == typeof(long))
    {
    long cValue = (long)keyValue;

    if(maxInt32 >= cValue)//没有溢出

    key.SetValue(valueName,(int)cValue );

    }
    else//超过了整数的范围,  实际更新值 = 数值(大于Int32. MaxValue)  - 4294967295 – 1
    {
    long newValue = cValue - maxDword - 1;
    int ss = (int)newValue;
    key.SetValue(valueName,ss);
    // string sValue = keyValue.ToString();//10进制数据
    // SystemType st = SystemTypeGetter.GetCurrentSystemType();
    // if(st == SystemType.WindowsXP || st == SystemType.Windows2003 || st > SystemType.Windows2003)
    // {
    // RegCommandHelper.SetValue(registryPath,valueName,RegistryValueKind.DWord,sValue);
    // }
    // else
    // {
    // throw new Exception("[郑佐]注:" + ".net Framework1.1不支持超过最大整数的DWORD设置!(这个可能为.net类库的bug.)");
    // }
    }
    return;
    }
    key.SetValue(valueName,keyValue);
    }
      

  5.   

    读取,在得到object数据后,你可以对该数据进行类型判断,确定其类型。
    ===============
    /// <summary>
    /// 获取键值
    /// </summary>
    /// <param name="registryPath">要打开的项的名称或路径</param>
    /// <param name="valueName">要检索的值的名称。</param>
    /// <returns>与 name 关联的数据;如果未找到 name,则为null</returns>
    public static object  GetValue(string registryPath ,string valueName)
    {
    RegistryKey key = GetRegistryKey(  registryPath, false);
    if(key == null)//健不存在
    return null;
    if(valueName == "@")
    valueName = String.Empty;
    object o = key.GetValue(valueName);
    if(o == null)//值不存在
    return null;
    //Dword处理
    //修正后值 = 4294967295  + 得到的数据(负数) + 1
    //十六进制字符串= (得到的数据 ).ToString("x8");不一定需要负数
    if(typeof(int) == o.GetType())
    {
    int nInt = (int)o;
    if(nInt < 0)
    {
    long newInt = ( maxDword  + (long)nInt + (long)1 );
    return newInt;
    }
    else
    {
    long newInt = (long)nInt;
    return newInt;
    }
    }
    return o;
    }
      

  6.   

    以上的代码在《敏捷注册表》软件中实现,
    具体可以看http://blog.csdn.net/zhzuo/category/139326.aspx
      

  7.   

    RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", true);            regkey.SetValue("StringKey", "string", RegistryValueKind.String);
                regkey.SetValue("BinaryKey", new byte[] { 10, 243, 34, 52, 64 }, RegistryValueKind.Binary);
                regkey.SetValue("DWORDKey", 234, RegistryValueKind.DWord);
      

  8.   

    .NET操作注册表很慢...还要申请权限...麻烦...