c#操作注册表的问题~~~~ 在c#对注册表的操作中,如何写入二进制值和dword值,如何读取这两个值?可以的话请附源码!谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 二进制没写过,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的习惯,估计应该是这样,也许猜得不对).因为我没试验过,不敢妄说,看看别人的回复吧. 这个很简单的,查一下MSDN吧,或者再这里搜索一下注册表 http://community.csdn.net/Expert/topic/5019/5019201.xml?temp=.5031702 写入===================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); } 读取,在得到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; } 以上的代码在《敏捷注册表》软件中实现,具体可以看http://blog.csdn.net/zhzuo/category/139326.aspx 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); .NET操作注册表很慢...还要申请权限...麻烦... 文件怎么样直接读取指定几行到几行的内容 C#使用DockPanel时,如何让停靠窗口时窗口的样式拖拽出来时也保持不变 如何判断DataGridview中当前单元格字符是不是空格? 自己写的一个控件无法使用 7*24小时运行程序如何处理异常 200分:讨论:事务的问题: 如何列举出网络上所有的SQL Server服务器 出错 WCF android 以及winform交互问题 ***** 业务对象成树型数据结构时发生的故事 ***** 请提供一个字符串分割的例子 关于Panel和窗体和问题 C#打印任意大小纸张的问题,请教遇到过此问题的前辈.
不过在2.0以后,多了个 Microsoft.Win32.RegistryValueKind枚举,可以让你选择是不是按这种格式写注册表.
我个人觉得,在1.1和1.0版中,写Object类型(实际是string或int的除外),应该是按Binary存储的,但Binary的内容可能是xml格式(这点是我通过.Net1.0或1.1写MSMQ联想到的,不过按.Net的习惯,估计应该是这样,也许猜得不对).因为我没试验过,不敢妄说,看看别人的回复吧.
===================
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);
}
===============
/// <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;
}
具体可以看http://blog.csdn.net/zhzuo/category/139326.aspx
regkey.SetValue("BinaryKey", new byte[] { 10, 243, 34, 52, 64 }, RegistryValueKind.Binary);
regkey.SetValue("DWORDKey", 234, RegistryValueKind.DWord);