程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 基於C#操作注冊表

基於C#操作注冊表

編輯:C#入門知識

1摘要
 2C#對注冊表操作進行了封裝;對Windows注冊表操作變得異常簡單。本程序實現了對Putty注冊表的創建,修改,刪除等操作,是一個不錯的應用范例。
 3介紹
 4C#的Registry類提供了Windows注冊表根項的RegistryKey對象,並提供靜態訪問項/值的方法。例如,對putty而言,我們的操作的根可選成:
 5RegistryKey rkRoot = Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("SimonTatham").OpenSubKey("PuTTY").OpenSubKey("Sessions");
 6
 7對session進行管理是程序的主要目標。
 8RegistryKey表示Windows注冊表中的項級節點;對值操作需要使用其方法,主要是:
 9OpenSubKey
10CreateSubKey
11SetValue
12GetValue
13Close
14修改項值
15實例代碼將所有基於rlogin的會話用戶/密碼改為root/123456。
16                    const string key1_name = "UserName";
17                    const string key2_name = "UserPasswd";
18
19                    RegistryKey rkRoot =
20                        Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("SimonTatham").OpenSubKey("PuTTY").OpenSubKey("Sessions");
21                    for (int i = 0; i < listBox4.SelectedItems.Count; i++)                                       
22                    {
23                        string ss = (string)listBox4.SelectedItems[i];
24                        listBox1.Items.Add("handling session:" + ss);
25                        RegistryKey rkSession = rkRoot.OpenSubKey(ss, RegistryKeyPermissionCheck.ReadWriteSubTree);
26                        //判斷是否為rlogin
27                        if (val_protocol == (string)rkSession.GetValue(key_protocol))
28                        {
29                            //修改用戶名/密碼
30                            rkSession.SetValue(key1_name, textBox1.Text);
31                            rkSession.SetValue(key2_name, textBox2.Text);
32                        }
33                        else
34                        {
35                            listBox1.Items.Add("does not handle " + ss + " because it is not a rlogin type session.");
36                        }
37                        rkSession.Close();
38                    }
39創建項值
40本程序支持將注冊表dump出來,並支持load,實際是創建。
41                        string sn = (string)listBox3.SelectedItems[i];
42                        RegistryKey rkSn = rkRoot.OpenSubKey(sn, RegistryKeyPermissionCheck.ReadWriteSubTree);
43                        listBox1.Items.Add("handling " + sn);
44                        ArrayList al = (ArrayList)session_datas[sn];
45                        for (int j = 0; j < al.Count; j++)
46                        {
47                            ArrayList items = (ArrayList)al[j];
48                            string subkey = (string)(items[0]);
49                            RegistryValueKind rvk = (RegistryValueKind)items[1];
50                            string v = (string)items[2];
51                            if (rvk == RegistryValueKind.DWord)
52                            {                               
53                             

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved