[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Registry equivalent of FileSystemWatcher class

Posted on 2005-05-13
1
Medium Priority
?
1,062 Views
Last Modified: 2012-05-05
I am using a windows service to monitor a directory but I also need to monitor a registry value.

Is there a way to monitor a change in a particular registry value in .NET similar to monitoring a file or directory with FileSystemWatcher?

If there is no .NET class that does this, can you give an example on how to do this?  
0
Comment
Question by:big_lew
1 Comment
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 14001057
you would just write a simple class that had a thread that checked the value and raised an event when it changed


      public class RegistryWatcher
      {
            public delegate void HandleRegistryChange(object sender, string key);
            public event HandleRegistryChange RegistryChanged;

            protected void OnRegistryChanged(object value)
            {
                  if(this.RegistryChanged != null)
                  {
                        this.RegistryChanged(this, key);
                  }
            }

            private void Loop()
            {
                  obj val;
                  while(true)
                  {
                        val = GetRegistryValue();
                        if(val != lastval)
                        {
                              this.OnRegistryChanged(val);
                              lastval = val;
                        }
                        Thread.Sleep(1000);
                  }
            }

            public void Start()
            {
                  ThreadStart ts = new ThreadStart(this.Loop);
                  Thread t = new Thread(ts);
                  t.Start();
            }

            public void RegistryWatcher() { }


      }



is an example (just implement GetRegistryValue) and it will produce events when it changes ...

Naturally it would be easy to have it watch 100 or 1000 keys if passed in (just another for loop)

Cheers,

Greg
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question