Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6124621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:09:13+00:00 2026-05-23T16:09:13+00:00

Currently I have a Windows Service which constantly monitors 4 folders. I have used

  • 0

Currently I have a Windows Service which constantly monitors 4 folders. I have used FileSystemWatchers to monitor folders.

But everytime a new folder is added, I have to uninstall the service add a new filesystemwatcher and then install the service.

I am thinking to make it dynamic or db driven where I dont not have to uninstall and re-install the service every time the program needs to monitor a new folder.

How can I achieve this?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:09:14+00:00Added an answer on May 23, 2026 at 4:09 pm

    Here you have a demo that uses XML with Linq2XML and has the check over the config file change:

    class Program
    {
        static List<FileSystemWatcher> _watchers = new List<FileSystemWatcher>();
        static bool _shouldReload = false;
    
        static void WaitReady(string fileName)
        {
            while (true)
            {
                try
                {
                    using (Stream stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        if (stream != null)
                        {
                            System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} ready.", fileName));
                            break;
                        }
                    }
                }
                catch (FileNotFoundException ex)
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
                }
                catch (IOException ex)
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
                }
                catch (UnauthorizedAccessException ex)
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
                }
                Thread.Sleep(500);
            }
        }
    
        static void Main(string[] args)
        {
            var configWatcher = new FileSystemWatcher(Path.GetDirectoryName(Path.GetFullPath("config.xml")), "config.xml");
            configWatcher.Changed += (o, e) =>
            {
                lock (_watchers)
                {
                    _watchers.ForEach(w => { w.EnableRaisingEvents = false; w.Dispose(); });
                    _watchers.Clear();
                }
    
                _shouldReload = true;
            };
    
            configWatcher.EnableRaisingEvents = true;
    
            Thread t = new Thread((ThreadStart)(() => {
                while (true)
                {
                    Thread.Sleep(5000); // reload only every five seconds (safety measure)
                    if (_shouldReload)
                    {
                        _shouldReload = false;
                        Console.WriteLine("Reloading configuration.");
                        WaitReady(Path.GetFullPath("config.xml"));
                        loadConfigAndRun();
                    }
                }
            }));
    
            t.IsBackground = true;
            t.Start();
    
            loadConfigAndRun();
    
            Console.ReadLine();
        }
    
        static void loadConfigAndRun()
        {
            var config = XElement.Load("config.xml").Elements();
            var paths = from watcher in config select watcher.Attribute("Folder").Value;
    
            foreach (var path in paths)
            {
                var watcher = new FileSystemWatcher(path);
                watcher.Created += new FileSystemEventHandler(watcher_Changed);
                watcher.Changed += new FileSystemEventHandler(watcher_Changed);
                watcher.Deleted += new FileSystemEventHandler(watcher_Changed);
                watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
                watcher.EnableRaisingEvents = true;
                _watchers.Add(watcher);
            }
        }
    
        static void toggleWatcher(string path)
        {
            var watcher = _watchers.FirstOrDefault(w => w.Path == path);
    
            if (watcher != null)
            {
                watcher.EnableRaisingEvents = !watcher.EnableRaisingEvents;
            }
        }
    
        static void watcher_Renamed(object sender, RenamedEventArgs e)
        {
            var watcher = sender as FileSystemWatcher;
    
            Console.WriteLine("Something renamed in " + watcher.Path);
        }
    
        static void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            var watcher = sender as FileSystemWatcher;
    
            Console.WriteLine("Something changed in " + watcher.Path);
        }
    }
    

    This is the config.xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <Watchers>
      <Watcher Folder="d:\ktm"></Watcher>
      <Watcher Folder="c:\windows"></Watcher>
    </Watchers>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a Windows Service which continually runs throughout the day. It has
I have created a windows service which is currently having three timers. First timer
Currently I have an assembly used for a windows UI application which got a
I'm currently in the process of creating a Windows service application which will monitor
We have a C# Windows service that currently processes all the PDFs by reading
Currently, I have a project with a Windows Service. I also created another Setup
Currently I have Windows App which is directly opening database connection (SQL Server is
I currently have a 32-bit .Net application (on x86 Windows) which require lots of
I have a Clustered Windows Hosting site which currently hosts an MVC Web Application
We are currently evaluating upgrading from XP to Windows 7, but have one last

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.