我在C#中开发一个注册表监控应用程序来创建一个注册表项。我尝试过使用p / invoke - 它创建了密钥,如果它不存在,但如果它确实存在则返回错误值5 - 访问被拒绝错误。
enum RegistryDispositionValue : uint
{
REG_CREATED_NEW_KEY = 0x00000001,
REG_OPENED_EXISTING_KEY = 0x00000002
}
[DllImport("advapi32.dll", SetLastError = true)]
private static extern int RegCreateKeyEx(
IntPtr hKey,
string lpSubKey,
uint reserved,
string lpClass,
uint dwOptions,
int samDesired,
IntPtr lpSecurityAttributes,
out IntPtr phkResult,
out RegistryDispositionValue lpdwDisposition);
int iResult;
IntPtr ipKey = IntPtr.Zero;
string _registrySubName="SOFTWARE\XYZ\subkey";
RegistryDispositionValue iDisposition;
int result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, _registrySubName, 0, null, REG_OPTION_VOLATILE , 0, IntPtr.Zero, out registryKey, out iDisposition);
AutoResetEvent _eventNotify = new AutoResetEvent(false);
WaitHandle[] waitHandles = new WaitHandle[] {_eventNotify, _eventTerminate};
while (!_eventTerminate.WaitOne(0, true))
{
result = RegNotifyChangeKeyValue(registryKey, true, _regFilter, _eventNotify.Handle, true);
if (result != 0)
throw new Win32Exception(result);
if (WaitHandle.WaitAny(waitHandles) == 0)
{
OnRegChanged();
}
}
我在这里缺少什么?
.net内置注册表类中是否有类似的API(RegNotifyChangeKeyValue())?
答案 0 :(得分:0)
试试这个。它似乎是一个非常流行的创建注册表监视器的解决方案: RegistryMonitor - a .NET wrapper class for RegNotifyChangeKeyValue