C#注册表的读,写,删除,查找
3,903 views
0
using system; using system.collections; using system.configuration; using system.data; using system.web; using system.web.security; using system.web.ui; using system.web.ui.htmlcontrols; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using microsoft.win32; public partial class register : system.web.ui.page { protected void page_load(object sender, eventargs e) { response.write("这里是读取到的信息"+"<br/>"); readregedit(); response.write("<br/>"); response.write("这里将要写入信息swort/swort-test" + "<br/>"); writeregedit(); response.write("写入结束" + "<br/>"); V];Eg. 4(Uy*Z= [ 本 资 料 来 源 于 贵 州 学 习 网 网络编程.Net教程 http://Www.gzU521.com ] V];Eg. 4(Uy*Z= response.write("查看存在与否" + "<br/>"); existsregedit(); response.write("查看结束" + "<br/>"); response.write("删除" + "<br/>"); deleteregedit(); response.write("删除结束" + "<br/>"); response.write("查看存在与否" + "<br/>"); existsregedit(); response.write("查看结束" + "<br/>"); } /// <summary> /// 注册表的读取 /// </summary> /// <returns></returns> public void readregedit() { registrykey rk = registry.currentuser; registrykey software = rk.opensubkey("software"); registrykey microsoft = software.opensubkey("microsoft"); registrykey windows = microsoft.opensubkey("windows"); registrykey current = windows.opensubkey("currentversion"); registrykey explorer = current.opensubkey("explorer"); registrykey shell = explorer.opensubkey(@"shell folders"); foreach (string b in shell.getvaluenames())//这里用shell.getvaluenames()不是shell.getsubkeynames() { response.write( b+" "+ shell.getvalue(b).tostring()); response.write("<br/>"); } } /// <summary> /// 注册表的写入 /// </summary> /// <returns></returns> public bool writeregedit() { try { registrykey rk = registry.currentuser; registrykey software = rk.opensubkey("software"); registrykey microsoft = software.opensubkey("microsoft"); registrykey windows = microsoft.opensubkey("windows"); registrykey current = windows.opensubkey("currentversion"); registrykey explorer = current.opensubkey("explorer"); registrykey shell = explorer.opensubkey(@"shell folders", true);//这里必须加true就是得到写入权限 registrykey key = shell.createsubkey("swort");//创建swort目录 key.setvalue("swort", "test"); //在swort目录下建立写入swort test response.write("写入成功!!!!!!!!!"); return true; } catch { return false; } } /// <summary> /// 注册表的删除 /// </summary> /// <returns></returns> public bool deleteregedit() { try { registrykey rk = registry.currentuser; registrykey software = rk.opensubkey("software"); registrykey microsoft = software.opensubkey("microsoft"); registrykey windows = microsoft.opensubkey("windows"); registrykey current = windows.opensubkey("currentversion"); registrykey explorer = current.opensubkey("explorer"); registrykey shell = explorer.opensubkey(@"shell folders",true); registrykey swort = shell.opensubkey("swort",true);//这里必须加true就是得到写入权限 swort.deletevalue("swort");//删除swort的值 这个连键值一起删除了 剩下一个 shell.deletesubkey("swort",false);//删除swort这个目录 要删除这个目录 必须具有权限 response.write("删除成功!!!!!!!!!"); return true; } catch { return false; } return true; } /// <summary> /// 查询某个键值是否存在 /// </summary> /// <returns></returns> public bool existsregedit() { registrykey rk = registry.currentuser; registrykey software = rk.opensubkey("software"); registrykey microsoft = software.opensubkey("microsoft"); registrykey windows = microsoft.opensubkey("windows"); registrykey current = windows.opensubkey("currentversion"); registrykey explorer = current.opensubkey("explorer"); registrykey shell = explorer.opensubkey(@"shell folders"); if (shell.subkeycount != 0) { registrykey swort = shell.opensubkey("swort"); foreach (string b in swort.getvaluenames()) { if (b == "swort") { response.write("存在这个键!"); return true; } } response.write("不存在这个键!"); return false; } else { response.write("不存在这个键!"); return false; } } }
原文: C#注册表的读,写,删除,查找
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2010-03-30 21:57:26
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!
转载注明: C#注册表的读,写,删除,查找 (米扑博客)