C#註冊表的讀,寫,刪除,尋找

來源:互聯網
上載者:User
C#註冊表的讀,寫,刪除,尋找

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


本文來自學習網(www.gzu521.com),原文地址:http://www.gzu521.com/campus/article/program/200809/176006.htm

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.