怎么读写注册表的?谁能给我个实例的哟~~~先谢谢了~~~在线等待~~
解决方案 »
- 分享个无敌解算术的代码.求测试(能解括号()/*-+逻辑运算符&|$)还有其他
- DataView.Sort问题
- C#做的登陆界面在检查输入的密码时就算输入正确也报出错
- 【求助】DOTNET中如何建立基类的问题?
- 在c#中,怎么能把pdf中的内容,读出到DataSet里面?
- 怎麼把datagrid的所有數据都導出到excel
- 求教根据两点经纬度得到球面距离算法
- 谈谈大家对ObjectSpaces的认识! :)
- 疑问:VC++6.0和C#.net2003能否协同开发软件
- 再来个简单的数据库连接。
- 如何通过注册表往程序里传递多个参数?
- C# 中如何实现遍历注册表的某个子键下的所有子键???求助各位虾哥虾姐了!!!! 在线急等!
http://community.csdn.net/Expert/topic/2894/2894881.xml?temp=.7155573
/// </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();
}好用就把分都给我吧
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;
}
}
}
}
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();