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 1084413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:33:31+00:00 2026-05-16T22:33:31+00:00

Below is my code for a File watcher class I wrote: class FileWatcher {

  • 0

Below is my code for a File watcher class I wrote:

class FileWatcher  
{  
    #region Method that begins watching  
    public static void watch()  
    {

        FileSystemWatcher watcher = new FileSystemWatcher();

        watcher.Path = ConfigurationManager.AppSettings["OpticusFileLoc"];
        watcher.Filter = ConfigurationManager.AppSettings["OpticusFileName"];

        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        watcher.Changed += new FileSystemEventHandler(OnChanged);
        Console.Write("\nWatcher started. Press any key to end.\n");

        watcher.EnableRaisingEvents = true;
    }
    #endregion

    #region Trigger function on change
    public static void OnChanged(object source, FileSystemEventArgs e)
    {
        Console.WriteLine("File has been changed.\n");
        //watcher.EnableRaisingEvents = false ;

        //Program.Main();
    }
    #endregion

}

How can I, from the OnChanged method, set the watcher.EnableRaisingEvents flag to false?

I guess I could do it by moving the declaration of the FileSystemWatcher object outside the method it is in, but I’m wondering if there’s another way to do it.

  • 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-16T22:33:32+00:00Added an answer on May 16, 2026 at 10:33 pm

    I guess I could do it by moving the
    declaration of the FileSystemWatcher
    object outside the method it is in,
    but I’m wondering if there’s another
    way to do it.

    That’s exactly how you should do it (have watcher just be a member field of your FileWatcher class).

    That said, you also could do it like this:

    public static void OnChanged(object source, FileSystemEventArgs e)
    {
        Console.WriteLine("File has been changed.");
    
        var watcher = source as FileSystemWatcher;
        if (watcher != null)
        {
            watcher.EnableRaisingEvents = false;
        }
        else
        {
            // Hmm... some other object called this method.
            // Do you really want that to be allowed?
        }
    }
    

    Notice the comment I added in the code above. Generally it’s a bad call to make methods such as this public, as this makes it possible for any arbitrary code to call OnChanged even when doing so makes no sense. (You want for this method to capture the event of a file being changed; if it’s actually callable from anywhere, then how do you know if the file was in fact changed or if this is just some random call from elsewhere?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below code uploads file on the FTP server public class UploadFile { static ResourceBundle
I am creating excel file using below code public class ResultSetToExcel { private HSSFWorkbook
How can declare template of a template class?? see below code: File: A.h class
I am reading a plist file using code below. My problem is that the
The code below demonstarates the problem: java 7 interfaces for watching file changes report
I just run the below code as a normal java class file and status.isReachable(3000)
am writing a java code that needs to save a file. the below code
folks, I have on my pages ( http://playdota.thilisar.cz ) a JavaScript file(code below), that
i write below code for sending id to another php file function selectCheckBox(k) {
I am trying to load a JS file using yepnope using the below code:

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.