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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:45:55+00:00 2026-06-16T16:45:55+00:00

class Program { FileSystemWatcher _watchFolder; string sPath = @D:\TestMonitor; static void Main(string[] args) {

  • 0
class Program
{
    FileSystemWatcher _watchFolder;
    string sPath = @"D:\TestMonitor";
    static void Main(string[] args)
    {
        Program p = new Program();
        Thread t = new Thread(new ThreadStart(p.startActivityMonitoring));
        t.Start();
    }


    private void startActivityMonitoring()
    {
        _watchFolder = new FileSystemWatcher();
        _watchFolder.Path = Convert.ToString(sPath);
        _watchFolder.NotifyFilter = System.IO.NotifyFilters.DirectoryName;
        _watchFolder.NotifyFilter =
        _watchFolder.NotifyFilter | System.IO.NotifyFilters.FileName;
        _watchFolder.NotifyFilter =
        _watchFolder.NotifyFilter | System.IO.NotifyFilters.Attributes;
        _watchFolder.Changed += new FileSystemEventHandler(eventRaised);
        _watchFolder.Created += new FileSystemEventHandler(eventRaised);
        _watchFolder.Deleted += new FileSystemEventHandler(eventRaised);
        _watchFolder.Renamed += new System.IO.RenamedEventHandler(eventRaised);
        _watchFolder.EnableRaisingEvents = true;
    }



    private void eventRaised(object sender, System.IO.FileSystemEventArgs e)
    {
        switch (e.ChangeType)
        {
            case WatcherChangeTypes.Changed:
                Console.WriteLine(string.Format("File {0} has been modified\r\n", e.FullPath));

                break;
            case WatcherChangeTypes.Created:
                Console.WriteLine(string.Format("File {0} has been created\r\n", e.FullPath));

                break;
            case WatcherChangeTypes.Deleted:
                Console.WriteLine(string.Format("File {0} has been deleted\r\n", e.FullPath));

                break;
            default: // Another action
                break;
        }
    }

}

A simple program using FileSystemWatcher to poll for changes inside a directory when i try to log the changes using Console.WriteLine it does not work though.

Not sure what is causing this issue since Console.WriteLine works well inside any thread

  • 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-06-16T16:45:56+00:00Added an answer on June 16, 2026 at 4:45 pm

    Your program is exiting right after it starts the thread. You need to keep the program running.
    One simple way would to be include a Console.ReadLine to stop the program from exiting.

    static void Main(string[] args)
        {
            Program p = new Program();
            Thread t = new Thread(new ThreadStart(p.startActivityMonitoring));
            t.Start();
            Console.Writeline("Press enter to exit");
            Console.ReadLine();
        }
    

    enter image description here

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

Sidebar

Related Questions

Source class Program { static void Main(string[] args) { var fw = new FileSystemWatcher(@M:\Videos\Unsorted);
class Program { static void Main(string[] args) { List<Book> books = new List<Book> {
I have the following code: class Program { static void Main(string[] args) { new
class Program { static void Main(string[] args) { var x = new Program(); Console.Write(x.Text);
class Program { static void Main(string[] args) { var dictionary = new Dictionary<string, int>()
class Program { static void Main(string[] args) { var getfiles = new fileshare.Program(); string
class Program { static void Main(string[] args) { A a = new A(); a.print();
class Program { static void Main(string[] args) { List<A> myList = new List<A> {new
class Program { static void Main(string[] args) { //GrandFather gf = new Son(); IGF<Father>
class Program { static void Main(string[] args) { Father objFather = new Son(); //Ok

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.