注册表观察程序 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/826971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Registry Watcher C#
提问by Andrew Ensley
I'm a newbie to WMI and I need to implement RegistryValueChangeEventin a C# service.
我是 WMI 的新手,我需要在 C# 服务中实现RegistryValueChangeEvent。
I need an event handler that gets triggered each time any one of a set of registry values is changed. I want behavior similar to the FileSystemWatcherclass's Changedevent, but for registry values.
我需要一个事件处理程序,每次更改一组注册表值中的任何一个时都会触发该事件处理程序。我想要类似于FileSystemWatcher类的Changed事件的行为,但对于注册表值。
If there's some other technique I could use to accomplish the same task, I'd appreciate that as well. My minimum requirement is that it be a better solution than what I have now: polling every 20 seconds and comparing the registry value with the last result.
如果我可以使用其他一些技术来完成相同的任务,我也会很感激。我的最低要求是它是比我现在拥有的更好的解决方案:每 20 秒轮询一次并将注册表值与上次结果进行比较。
Please provide example code in your answer. If I can get an example for watching just one registry value, that would be fine.
请在您的答案中提供示例代码。如果我能得到一个仅查看一个注册表值的示例,那就没问题了。
I need a solution in .Net 2.0
我需要 .Net 2.0 中的解决方案
Thanks.
谢谢。
采纳答案by Ed Altorfer
WMI can sometimes be interesting to work with...I think I understand your question, so take a look at the code snippet below and let me know if it's what you're looking for.
使用 WMI 有时会很有趣...我想我理解您的问题,所以请查看下面的代码片段,让我知道它是否是您要查找的内容。
// ---------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="">
//
// </copyright>
// <summary>
// Defines the WmiChangeEventTester type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace WmiExample
{
using System;
using System.Management;
/// <summary>
/// </summary>
public class WmiChangeEventTester
{
/// <summary>
/// Initializes a new instance of the <see cref="WmiChangeEventTester"/> class.
/// </summary>
public WmiChangeEventTester()
{
try
{
// Your query goes below; "KeyPath" is the key in the registry that you
// want to monitor for changes. Make sure you escape the \ character.
WqlEventQuery query = new WqlEventQuery(
"SELECT * FROM RegistryValueChangeEvent WHERE " +
"Hive = 'HKEY_LOCAL_MACHINE'" +
@"AND KeyPath = 'SOFTWARE\Microsoft\.NETFramework' AND ValueName='InstallRoot'");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
Console.WriteLine("Waiting for an event...");
// Set up the delegate that will handle the change event.
watcher.EventArrived += new EventArrivedEventHandler(HandleEvent);
// Start listening for events.
watcher.Start();
// Do something while waiting for events. In your application,
// this would just be continuing business as usual.
System.Threading.Thread.Sleep(100000000);
// Stop listening for events.
watcher.Stop();
}
catch (ManagementException managementException)
{
Console.WriteLine("An error occurred: " + managementException.Message);
}
}
/// <summary>
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private void HandleEvent(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Received an event.");
// RegistryKeyChangeEvent occurs here; do something.
}
/// <summary>
/// </summary>
public static void Main()
{
// Just calls the class above to check for events...
WmiChangeEventTester receiveEvent = new WmiChangeEventTester();
}
}
}
回答by marcc
You'll need to utilize WMI for it. See http://msdn.microsoft.com/en-us/library/aa393035.aspx
您需要为此使用 WMI。请参阅http://msdn.microsoft.com/en-us/library/aa393035.aspx
回答by felixg
Are you limited to WMI? If not you can use RegNotifyChangeKeyValuewrappers like RegistryMonitor
您是否仅限于 WMI?如果不是,您可以使用RegNotifyChangeKeyValue包装器,例如RegistryMonitor
回答by Dan
You really don't need WMI, as others have pointed out. I too have used RegistryMonitorwith no problems.
正如其他人指出的那样,您确实不需要 WMI。我也使用RegistryMonitor没有任何问题。
If you need an example, there's already example code for the RegistryMonitor on the page itself. Did you scroll down to this bit on the code project:
如果您需要一个示例,页面上已经有 RegistryMonitor 的示例代码。您是否在代码项目中向下滚动到这一点:
public class MonitorSample
{
static void Main()
{
RegistryMonitor monitor = new
RegistryMonitor(RegistryHive.CurrentUser, "Environment");
monitor.RegChanged += new EventHandler(OnRegChanged);
monitor.Start();
while(true);
monitor.Stop();
}
private void OnRegChanged(object sender, EventArgs e)
{
Console.WriteLine("registry key has changed");
}
}