C# 操作注册表函数

using System; 
using System.Collections.Generic; 
using System.Text; 
using Microsoft.Win32; 
//对注册表操作 
using System.Collections; 
//使用Arraylist 
using System.Security.Cryptography;
//加密解密 
using System.IO; 
//文件操作 
using System.Runtime.InteropServices;
//调用DLL DllImport 
using System.Management; 
//获取硬件信息 
using System.Net; 
//获取IP地址是用到 
using System.Drawing; 
//image 
using System.Net.NetworkInformation; 
//ping 用到 
using System.Text.RegularExpressions; 
//正则 
using System.Data; 
using System.Data.SqlClient; 
using Microsoft.VisualBasic; 
//简体转繁体时用到 
using System.Web; 
//html 
UrlEncode 
//注册表操作 
public class GF_RegReadWrite { 
	///<summary> /// 读取路径为keypath,键名为keyname的注册表键值,缺省返回def /// </summary> 
	///<param name="rootkey"></param> 
	///<param name="keypath">路径</param> 
	///<param name="keyname">键名</param> 
	///<param name="rtn">默认为null</param> 
	///<returns></returns> 
	static public bool GetRegVal(RegistryKey rootkey, string keypath, string keyname, out string rtn) { 
		rtn = ""; 
		try { 
			RegistryKey key = rootkey.OpenSubKey(keypath); 
			rtn = key.GetValue(keyname).ToString(); 
			key.Close(); return true; 
		} catch { 
			return false; 
			} 
		} 
	/// <summary> /// 设置路径为keypath,键名为keyname的注册表键值为keyval /// </summary> 
	/// <param name="rootkey"></param> 
	/// <param name="keypath"></param> 
	/// <param name="keyname"></param> 
	/// <param name="keyval"></param> 
	/// <returns></returns> 
	static public bool SetRegVal(RegistryKey rootkey, string keypath, string keyname, string keyval) { 
		try { 
			RegistryKey key = rootkey.OpenSubKey(keypath, true); 
			if (key == null) 
				key = rootkey.CreateSubKey(keypath); 
			key.SetValue(keyname, (object)keyval); 
			key.Close(); return true; 
		} catch { 
			return false; 
		} 
	} 
	/// 创建路径为keypath的键 
	private RegistryKey CreateRegKey(RegistryKey rootkey, string keypath) { 
		try { 
			return rootkey.CreateSubKey(keypath); 
		} catch { 
			return null; 
		} 
	} 
	/// 删除路径为keypath的子项 
	private bool DelRegSubKey(RegistryKey rootkey, string keypath) { 
		try { 
			rootkey.DeleteSubKey(keypath); 
			return true; 
		} catch { 
			return false; 
		} 
	} 
	/// 删除路径为keypath的子项及其附属子项 
	private bool DelRegSubKeyTree(RegistryKey rootkey, string keypath) { 
		try { 
			rootkey.DeleteSubKeyTree(keypath); 
			return true; 
		} catch { 
			return false; 
		} 
	} 
	/// 删除路径为keypath下键名为keyname的键值 
	private bool DelRegKeyVal(RegistryKey rootkey, string keypath, string keyname) {
		try {
			RegistryKey key = rootkey.OpenSubKey(keypath, true); 
			key.DeleteValue(keyname); 
			return true;
		} catch {
			return false;
		} 
	} 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木叶流丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值