C#监视注册表的类 使用WIN32 API

19 篇文章 0 订阅

使用

引用 Microsoft.Win32;

 

  1.  private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             Microsoft.Win32.RegistryKey _Key = Microsoft.Win32.Registry.CurrentUser;
  4.             _Key = _Key.OpenSubKey("SoftWare");
  5.             _Key = _Key.OpenSubKey("Microsoft");
  6.             _Key = _Key.OpenSubKey("Windows");
  7.             _Key = _Key.OpenSubKey("CurrentVersion");
  8.             _Key = _Key.OpenSubKey("Run");
  9.             T = new Zgke.Copy.MonitorWindowsReg(_Key);
  10.             T.UpReg += new Zgke.Copy.MonitorWindowsReg.UpdataReg(T__UpdateReg);
  11.             T.Star();          
  12.         }
  13.         void T__UpdateReg(string OldText, object OldValue, string NewText, object NewValue)
  14.         {
  15.             object Old = OldValue;
  16.             object New = NewValue;
  17.             if (Old == null) Old = "";
  18.             if (New == null) New = "";
  19.             MessageBox.Show(OldText + ":" + Old.ToString(), NewText + ":" + New.ToString());
  20.         }             
  21.         private void button2_Click(object sender, EventArgs e)
  22.         {
  23.             T.Stop();
  24.         }

下面是具体的类

 

  1.   /// <summary>
  2.     /// 注册表监视类
  3.     /// zgke@Sina.com
  4.     /// qq:116149 
  5.     /// </summary>
  6.     public class MonitorWindowsReg
  7.     {
  8.         [DllImport("advapi32.dll", EntryPoint = "RegNotifyChangeKeyValue")]
  9.         private static extern int RegNotifyChangeKeyValue(IntPtr hKey,bool bWatchSubtree,int dwNotifyFilter,int hEvent,bool fAsynchronus);
  10.         [DllImport("advapi32.dll", EntryPoint = "RegOpenKey")]
  11.         private static extern int RegOpenKey(uint hKey, string lpSubKey, ref IntPtr phkResult);
  12.         [DllImport("advapi32.dll", EntryPoint = "RegCloseKey")]
  13.         private static extern int RegCloseKey(IntPtr hKey);
  14.         private static uint HKEY_CLASSES_ROOT = 0x80000000;
  15.         private static uint HKEY_CURRENT_USER = 0x80000001;
  16.         private static uint HKEY_LOCAL_MACHINE = 0x80000002;
  17.         private static uint HKEY_USERS = 0x80000003;
  18.         private static uint HKEY_PERFORMANCE_DATA = 0x80000004;
  19.         private static uint HKEY_CURRENT_CONFIG = 0x80000005;
  20.         private static uint HKEY_DYN_DATA = 0x80000006;
  21.         private static int REG_NOTIFY_CHANGE_NAME = 0x1;
  22.         private static int REG_NOTIFY_CHANGE_ATTRIBUTES = 0x2;
  23.         private static int REG_NOTIFY_CHANGE_LAST_SET = 0x4;
  24.         private static int REG_NOTIFY_CHANGE_SECURITY = 0x8;
  25.         /// <summary>
  26.         /// 打开的注册表句饼
  27.         /// </summary>
  28.         private IntPtr _OpenIntPtr = IntPtr.Zero;
  29.         private RegistryKey _OpenReg;
  30.         private Hashtable _Date = new Hashtable();
  31.         /// <summary>
  32.         /// 监视注册表 
  33.         /// </summary>
  34.         /// <param name="MonitorKey">Microsfot.Win32.RegistryKey</param>
  35.         public MonitorWindowsReg(RegistryKey MonitorKey)
  36.         {
  37.             if (MonitorKey == nullthrow new Exception("注册表参数不能为NULL");
  38.             _OpenReg = MonitorKey;
  39.             string[] _SubKey=MonitorKey.Name.Split('//');
  40.             uint _MonitorIntPrt = HKEY_CURRENT_USER;
  41.             switch (_SubKey[0])
  42.             {
  43.                 case "HKEY_CLASSES_ROOT":
  44.                     _MonitorIntPrt = HKEY_CLASSES_ROOT;
  45.                     break;
  46.                 case "HKEY_CURRENT_USER":
  47.                     _MonitorIntPrt = HKEY_CURRENT_USER;
  48.                     break;
  49.                 case "HKEY_LOCAL_MACHINE":
  50.                     _MonitorIntPrt = HKEY_LOCAL_MACHINE;
  51.                     break;
  52.                 case "HKEY_USERS":
  53.                     _MonitorIntPrt = HKEY_USERS;
  54.                     break;
  55.                 case "HKEY_PERFORMANCE_DATA":
  56.                     _MonitorIntPrt = HKEY_PERFORMANCE_DATA;
  57.                     break;
  58.                 case "HKEY_CURRENT_CONFIG":
  59.                     _MonitorIntPrt = HKEY_CURRENT_CONFIG;
  60.                     break;
  61.                 case "HKEY_DYN_DATA":
  62.                     _MonitorIntPrt = HKEY_DYN_DATA;
  63.                     break;
  64.                 default:
  65.                     break;
  66.             }
  67.             string _Text = MonitorKey.Name.Remove(0, MonitorKey.Name.IndexOf('//')+1); 
  68.             RegOpenKey(_MonitorIntPrt, _Text, ref _OpenIntPtr);   
  69.         }
  70.         /// <summary>
  71.         /// 开始监控
  72.         /// </summary>
  73.         public void Star()
  74.         {
  75.             if (_OpenIntPtr == IntPtr.Zero)
  76.             {
  77.                 throw new Exception("不能打开的注册项!");
  78.             }
  79.             GetOldRegData();
  80.            
  81.             System.Threading.Thread _Thread = new System.Threading.Thread(new System.Threading.ThreadStart(Monitor));
  82.             StarMonitor = true;
  83.             _Thread.Start();
  84.         }
  85.         /// <summary>
  86.         /// 更新老的数据表
  87.         /// </summary>
  88.         private void GetOldRegData()
  89.         {
  90.             _Date.Clear();
  91.             string[] OldName = _OpenReg.GetValueNames();
  92.             for (int i = 0; i != OldName.Length; i++)
  93.             {
  94.                 _Date.Add(OldName[i], _OpenReg.GetValue(OldName[i]));
  95.             }
  96.         }
  97.         /// <summary>
  98.         /// 停止监控
  99.         /// </summary>
  100.         public void Stop()
  101.         {
  102.             StarMonitor = false;                      
  103.             RegCloseKey(_OpenIntPtr);
  104.         }
  105.         /// <summary>
  106.         /// 停止标记
  107.         /// </summary>
  108.         private bool StarMonitor = false;
  109.         /// <summary>
  110.         /// 开始监听
  111.         /// </summary>
  112.         public void Monitor()
  113.         {
  114.             while (StarMonitor)
  115.             {
  116.                 RegNotifyChangeKeyValue(_OpenIntPtr, false, REG_NOTIFY_CHANGE_NAME + REG_NOTIFY_CHANGE_ATTRIBUTES + REG_NOTIFY_CHANGE_LAST_SET + REG_NOTIFY_CHANGE_SECURITY, 0, false);
  117.                 GetUpdate();
  118.             }
  119.         }
  120.         /// <summary>
  121.         /// 检查数据
  122.         /// </summary>
  123.         private void GetUpdate()
  124.         {
  125.             string[] NewName = _OpenReg.GetValueNames();  //获取当前的名称
  126.             if (NewName.Length < _Date.Count)    //如果当前少了说明是删除
  127.             {
  128.                 foreach (string Key in _Date.Keys)  //循环刚开始的记录的名称
  129.                 {
  130.                     bool _Del = true;
  131.                     for (int i = 0; i != NewName.Length; i++)     //循环比较
  132.                     {
  133.                         if (Key == NewName[i])   
  134.                         {
  135.                             _Del = false;
  136.                             break;
  137.                         }
  138.                     }
  139.                     if (_Del == true)
  140.                     {
  141.                         if (UpReg != null) UpReg(Key, _Date[Key], """");      //删除 
  142.                         GetOldRegData(); 
  143.                         return;
  144.                     }
  145.                 }
  146.             }
  147.             for (int i = 0; i != NewName.Length; i++)
  148.             {
  149.                 if (_Date[NewName[i]] == null)
  150.                 {
  151.                     if (UpReg != null) UpReg("""", NewName[i], _OpenReg.GetValue(NewName[i]));   //添加
  152.                     GetOldRegData();
  153.                     return;
  154.                 }
  155.                 else
  156.                 {
  157.                     if (_Date[NewName[i]].ToString() == _OpenReg.GetValue(NewName[i]).ToString()) continue;
  158.                     //修改
  159.                     if (UpReg != null) UpReg(NewName[i], _Date[NewName[i]], NewName[i], _OpenReg.GetValue(NewName[i]));
  160.                     GetOldRegData();
  161.                     return;
  162.                 }
  163.             }
  164.         }
  165.         public delegate void UpdataReg(string OldText,object OldValue,string NewText,object NewValue);
  166.         public event UpdataReg UpReg;
  167.    
  168.     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值