C#注册表的读,写,删除,查找

版权声明:本文为米扑博客原创文章,未经米扑博客允许不得转载。 https://blog.csdn.net/sunboy_2050/article/details/5434789

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; 
        } 
    } 
} 

展开阅读全文

没有更多推荐了,返回首页