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

  • SEARCH
  • Home
  • 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 6937937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:25:40+00:00 2026-05-27T12:25:40+00:00

I got a problem similar to this one: FileSystemWatcher – only the change event

  • 0

I got a problem similar to this one: FileSystemWatcher – only the change event once firing once?

But since that thread is two years old and my code is a bit different, I decided to open a new question.

Well, here’s my code:

while (true)
{
  FileSystemWatcher fw = new FileSystemWatcher();

  fw.Path = @"Z:\";
  fw.Filter = "*.ini";
  fw.WaitForChanged(WatcherChangeTypes.All);
  Console.WriteLine("File changed, starting script...");
  //if cleanup
  try
  {
      if (File.ReadAllLines(@"Z:\file.ini")[2] == "cleanup")
      {
          Console.WriteLine("Cleaning up...");
          Process c = new Process();
          c.StartInfo.FileName = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim('\\') + @"\clean.exe";
          c.StartInfo.WorkingDirectory = System.Environment.SpecialFolder.DesktopDirectory.ToString();
          c.Start();
          c.WaitForExit();
          Console.WriteLine("Done with cleaning up, now starting script...");
      }
  }
  catch
  {
      Console.WriteLine("No cleanup parameter found.");
  }
  Process p = new Process();
  p.StartInfo.FileName = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim('\\') + @"\go.exe";
  p.StartInfo.WorkingDirectory = System.Environment.SpecialFolder.DesktopDirectory.ToString();
  p.Start();
  Console.WriteLine("Script running...");
  p.WaitForExit();
  fw = null;
  Console.WriteLine("Done. Waiting for next filechange...");
}

Problem: This program should detect a file change in the file “Z:\file.ini”. If it has changed, a script should be fired. When the script is done, the programm should return to the start and start watching for changes, again (that’s why I used the while-loop).
Well, the first change is detected and everything seems to be working just fine, but any changes AFTER the first one are not going to be detected. I tried to set the FileSystemWatcher Object to null, as you can see, but it didn’t help.

So, I hope for good answers. Thanks.

  • 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-27T12:25:41+00:00Added an answer on May 27, 2026 at 12:25 pm

    I would change your design so you don’t rely on the FileSystemWatcher for any changes. Instead poll the directory or the file that your watching for any changes. You can then use the FileSystemWatcher in conjunction with this to wake it up as soon as possible if we know there are changes. This way, if you miss an event, you’d still recover from it based on your poll time-out.

    e.g.

    static void Main(string[] args)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(@"f:\");
        ManualResetEvent workToDo = new ManualResetEvent(false);
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Changed += (source, e) => { workToDo.Set(); };
        watcher.Created += (source, e) => { workToDo.Set(); };
    
        // begin watching
        watcher.EnableRaisingEvents = true;
    
        while (true)
        {
            if (workToDo.WaitOne())
            {
                workToDo.Reset();
                Console.WriteLine("Woken up, something has changed.");
            }
            else
                Console.WriteLine("Timed-out, check if there is any file changed anyway, in case we missed a signal");
    
            foreach (var file in Directory.EnumerateFiles(@"f:\")) 
                Console.WriteLine("Do your work here");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem similar to,but subtly different from, that described here (Loading assemblies
Firslty, I'm aware of some similar questions along the lines of this one, but
I've got a slightly tricky problem to solve; imagine this: One of my applications
I got a similar problem to this guy while processing 4MB log file. Actually
This question may be very similar to my one, but I cannot see the
Hey, I have a problem similar to this one ( click ). Approx 2
I'm having a problem with a task similar to this one: click (translated) (the
I've got a .php page showing a HTML table similar to this one :-
My question is similar to this one but I'm having some problems with the
My problem is very similar to eight queens puzzle. I've got 2-dimensional array (N

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.