程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#小軟件(SaveClassic)開發手記--(3)基礎類(注冊表操作類RegEdit)

c#小軟件(SaveClassic)開發手記--(3)基礎類(注冊表操作類RegEdit)

編輯:C#入門知識

 

   該類主要實現了注冊表的修改,代碼也是大白話,很簡單,使用方法在窗體開發中也會用到的。具體代碼如下所示。

 1 using Microsoft.Win32;

 2 namespace Common

 3 {

 4     public class RegEdit

 5     {

 6         public RegEdit()

 7         {

 8         }

 9 //獲取注冊表中的數據

10         public string GetRegistData(string name)

11         {

12             string registData;

13             RegistryKey hkml = Registry.LocalMachine;

14             RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);

15             RegistryKey aimdir = software.OpenSubKey("Microsoft", true);

16             registData = aimdir.GetValue(name).ToString();

17             return registData;

18         }

19 //寫入注冊表數據 www.2cto.com

20         public void WTRegedit(string name, string tovalue)

21         {

22             RegistryKey hklm = Registry.LocalMachine;

23             RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);

24             RegistryKey aimdir = software.CreateSubKey("XXX");

25             aimdir.SetValue(name, tovalue);

26         }

27 //刪除注冊表數據

28         public void DeleteRegist(string name)

29         {

30             string[] aimnames;

31             RegistryKey hkml = Registry.LocalMachine;

32             RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);

33             RegistryKey aimdir = software.OpenSubKey("XXX", true);

34             aimnames = aimdir.GetSubKeyNames();

35             foreach (string aimKey in aimnames)

36             {

37                 if (aimKey == name)

38                     aimdir.DeleteSubKeyTree(name);

39             }

40         }

41

42 //判斷注冊表中數據是否存在

43         public bool IsRegeditExit(string name)

44         {

45             bool _exit = false;

46             string[] subkeyNames;

47             RegistryKey hkml = Registry.LocalMachine;

48             RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);

49             RegistryKey aimdir = software.OpenSubKey("Microsoft", true);

50             subkeyNames = aimdir.GetSubKeyNames();

51             foreach (string keyName in subkeyNames)

52             {

53                 if (keyName == name)

54                 {

55                     _exit = true;

56                     return _exit;

57                 }

58             }

59             return _exit;

60         }

61     }

62 }

作者 zhaoyang

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