∈鱼杆 ---我的鱼塘

执着,坚定,友爱,勇敢(www.pumaboyd.com)Live Message

导航

Taskvison 读书笔记(二) C# 注册表操作

Posted on 2006-09-12 10:49  ∈鱼杆  阅读(891)  评论(1编辑  收藏  举报

类前添加如下命名空间

using Microsoft.Win32;

添加/更新注册表:

/// <summary>

/// 添加注册表的值

/// </summary>

/// <param name="subKey"></param>

public void SaveRegistry(string subKey,string strName,string strValue)

{

     RegistryKey regKey = Registry.CurrentUser.CreateSubKey(subKey);

     regKey.SetValue(strName,strValue);

     regKey.Close();

}

读注册表:

/// <summary>

/// 读取注册表

/// </summary>

/// <param name="subKey"></param>

/// <param name="strName"></param>

/// <returns></returns>

public object ReadRegistry(string subKey,string strName)

{

     RegistryKey regKey = Registry.CurrentUser.OpenSubKey(subKey);

     object objtemp ;

     if (regKey == null)

     {   

         return  "";

     }

     if(regKey.GetValue(strName) != null)

     {

         objtemp = regKey.GetValue(strName);

     }

     else

     {

         objtemp = "";

     }

     regKey.Close();

     return objtemp;

}

删除注册表根节点:

/// <summary>

/// 删除节点

/// </summary>

/// <param name="strKey"></param>

public void DeleteRegistry(string strKey)

{

     Registry.CurrentUser.DeleteSubKey(strKey);

}

删除注册表的值:

/// <summary>

/// 删除键值

/// </summary>

/// <param name="subKey"></param>

/// <param name="strName"></param>

public void DeleteSubRegistry(string subKey,string strName)

{

     RegistryKey regKey = Registry.CurrentUser.CreateSubKey(subKey);

     regKey.DeleteValue(strName,false);

}



源码下载:
/Files/pumaboyd/Registry.rar