Click here to Skip to main content
16,016,783 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Currently i am using filesystemwatcher class to watch the file changes But in my case i have millions of file on different- different location in the system, so i have to create millions instance of fileststemwatcher class which is causing the performance issue.

Please tell how can i maximize the performance or is there another way to monitor the all files in c#.
Posted

1 solution

The below given code will help you monitor all .txt files inside a given folder. It will monitor all .txt file in the given folder and subfolders. For you , if you have to monitor all .txt files in "C" drive, then give the path for FileSystemWatcher accordingly and set the filter to "*.txt" (Filter = "*.txt";) and set IncludeSubdirectories = true. It will then fire method "Action" for the changes to any .txt files.


C#
public static void Main(string[] arg)
{
    FileSystemWatcher watcher = new FileSystemWatcher(@"C:\testFolder")
                                    {
                                        IncludeSubdirectories = true,

                                        NotifyFilter = NotifyFilters.LastAccess |NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,

                                        Filter = "*.txt"
                                    };



    // Add event handlers.
    watcher.Changed += Action;
    watcher.Created += Action;
    watcher.Deleted += Action;
    watcher.Renamed += Action;

    // Begin watching.
    watcher.EnableRaisingEvents = true;

    Console.ReadLine();

}
public static void Action(object obj, FileSystemEventArgs fileInfo)
{
    Console.WriteLine(fileInfo.FullPath.ToString() + fileInfo.ChangeType);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900