搞c#3周了,还是用起来觉得不顺。自己写不出来新东西,在codeproject上找了个监测注册表的,结果在.net 2.0上跑不起来,结果问问作者,说是2.0和1.0中的有点出入,结果当时没有来得及看到,导致我自己完全pinvoke了一个类。所以贴出来让大家批判一下,老风格:贴代码,很简单,不做说明了。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
namespace hhRegMonitor
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//监测的类型
public class RegChangeNotifyFilter
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static long Key
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return 0x00000001L; } // REG_NOTIFY_CHANGE_NAME = 1,
}
![](/Images/OutliningIndicators/InBlock.gif)
public static long Attribute
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return 0x00000002L; } // REG_NOTIFY_CHANGE_ATTRIBUTES = 2,
}
![](/Images/OutliningIndicators/InBlock.gif)
public static long Value
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return 0x00000004L; }// REG_NOTIFY_CHANGE_LAST_SET = 4,
}
public static long Security
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return 0x00000008L; } //REG_NOTIFY_CHANGE_SECURITY = 8,
}
}
![](/Images/OutliningIndicators/InBlock.gif)
//监测的根
public class RegRootKey
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static long HKEY_LOCAL_MACHINE
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return 0x80000002L;}
}
public static long HKEY_CURRENT_USER
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return 0x80000001L; }
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public class RegistryMonitor
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public RegistryMonitor()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](/Images/OutliningIndicators/InBlock.gif)
//输出错误信息
public event System.IO.ErrorEventHandler MonitorError;
![](/Images/OutliningIndicators/InBlock.gif)
//事件发生时需要激发的函数
public event EventHandler RegChanged;
![](/Images/OutliningIndicators/InBlock.gif)
public RegistryMonitor(long pfilter,
long proot, string psubkey)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_subkey = psubkey;
_rootkey = proot;
_filter = pfilter;
}
![](/Images/OutliningIndicators/InBlock.gif)
private void WaitForChange()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IntPtr myKey;
![](/Images/OutliningIndicators/InBlock.gif)
unchecked
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RegOpenKey(new IntPtr((int)_rootkey), _subkey, out myKey);
RegNotifyChangeKeyValue(myKey, true,
(int)_filter, _eventhandle, true);
![](/Images/OutliningIndicators/InBlock.gif)
WaitForSingleObject(_eventhandle, INFINITE);
}
}
catch (Exception e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (MonitorError != null)
MonitorError(this, new System.IO.ErrorEventArgs(e));
}
}
public bool Monitoring()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _notirythread != null;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void StartMonitor()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_notirythread != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException("Monitoring
..");
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_notirythread = new Thread(new ThreadStart(ThreadLoop));
_notirythread.IsBackground = true;
_notirythread.Start();
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public void StopMonitor()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_notirythread.Join();
_notirythread = null;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public long Filter
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _filter; }
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_notirythread == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_filter = value;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException("Monitoring
..");
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public long Root
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _rootkey; }
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_notirythread == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_rootkey = value;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException("Monitoring
..");
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public string SubKey
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _subkey; }
![](/Images/OutliningIndicators/InBlock.gif)
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_notirythread == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_subkey = value;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException("Monitoring
..");
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private void ThreadLoop()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while (_notirythread != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WaitForChange();
if (RegChanged != null)
RegChanged(this, EventArgs.Empty);
}
}
catch (Exception e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (MonitorError != null)
MonitorError(this, new System.IO.ErrorEventArgs(e));
![](/Images/OutliningIndicators/InBlock.gif)
_notirythread = null;
}
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
DllImport#region DllImport
[DllImport("kernel32.dll", EntryPoint = "CreateEvent")]
static extern IntPtr CreateEvent(IntPtr eventAttributes,
bool manualReset, bool initialState, String name);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("advapi32.dll", EntryPoint = "RegOpenKey")]
static extern IntPtr RegOpenKey(IntPtr key, String subKey,
out IntPtr resultSubKey);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("advapi32.dll", EntryPoint = "RegNotifyChangeKeyValue")]
static extern long RegNotifyChangeKeyValue(IntPtr key,
bool watchSubTree, int notifyFilter, IntPtr regEvent, bool
async);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll", EntryPoint = "WaitForSingleObject")]
static extern long WaitForSingleObject(IntPtr handle, int timeOut);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll", EntryPoint = "CloseHandle")]
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
some private members#region some private members
private static extern IntPtr CloseHandle(IntPtr handle);
![](/Images/OutliningIndicators/InBlock.gif)
private IntPtr _eventhandle = CreateEvent((IntPtr)null, false, false, "hhregmonitor");
![](/Images/OutliningIndicators/InBlock.gif)
private long _filter = 0;
private long _rootkey = 0;
private const int INFINITE = -1;
private Thread _notirythread = null;
//要监测的路径
private string _subkey = "";
#endregion
}
}
![](/Images/OutliningIndicators/None.gif)
测试demo
class Program
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
static void Main(string[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
testmonitor test = new testmonitor();
test.monitorreg();
Console.Read();
}
}
public class testmonitor
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private RegistryMonitor registryMonitor = null;
public testmonitor()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
public void monitorreg()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
registryMonitor = new RegistryMonitor(RegChangeNotifyFilter.Key,
RegRootKey.HKEY_LOCAL_MACHINE,
"software");
registryMonitor.RegChanged += new EventHandler(OnRegChanged);
![](/Images/OutliningIndicators/InBlock.gif)
registryMonitor.MonitorError += new System.IO.ErrorEventHandler(OnError);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
registryMonitor.StartMonitor();
}
private void OnRegChanged(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.Write("change ");
}
private void OnError(object sender, System.IO.ErrorEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.Write("Regmonitor Err: " + e.GetException().Message);
} 类结构参考
http://www.codeproject.com/csharp/registrymonitor.asp