C # method of monitoring windows folder

Time:2021-1-27

Are you worried about not being able to see the pictures stored on the computer by your children, or are you worried about not being able to monitor the things stored on the computer by your employees? The product recommended to you today is absolutely your best choice. It is a product produced by a large American factory, which fully meets the international standards, and perfectly supports the system above Windows 98. It is the file system watcher Brand monitor. He listens for notification of file system changes and raises events when a directory or a file in a directory changes. Now let’s take a look at his details.

Zero, detail feature

1. The common methods are as follows:

OnChanged(FileSystemEventArgs)
This event is called when the size of the file or directory in the monitored directory, system properties, last write time, last access time, or security permissions are changed.
OnCreated(FileSystemEventArgs)
This event is called when a file or directory is created in the monitored directory. It should be noted that although copy and move are not created, they will also trigger this event. If a file is copied or moved to a monitored directory, the oncreated event is immediately raised, followed by one or more onchanged events.
OnDeleted(FileSystemEventArgs)
This event is penalized when a file or directory in a monitored directory is deleted. This event is also triggered when we cut files from the monitored directory.
OnRenamed(RenamedEventArgs)
This method is called when you rename a file or directory in the monitored directory.

2. Common attributes are:

Filter: gets or sets the filter string used to determine which files to monitor in the directory.
Include subdirectories: gets or sets whether to monitor the subdirectories of the monitored directory.
Internalbuffersize: internal buffer size, the maximum is 64K, the default is 8K.
Notifyfilter: gets or sets which contents need to be captured after being changed. The types that can be set are as follows:

Path: the path of the folder to be monitored

1、 Examples

using System;
using System.IO;
using static System.Console;
namespace FileSystemWatcherDemo
{
 class Program
 {
  static void Main(string[] args)
  {
   FileSystemWatcher fsw = new FileSystemWatcher();
   fsw.Path = @"d:\test";
   fsw.Filter = "*.txt";
   fsw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.FileName;
   fsw.IncludeSubdirectories = true;
   fsw.InternalBufferSize = 64 * 1024;
   fsw.Created += Fsw_Created;
   fsw.Changed += Fsw_Changed;
   fsw.Deleted += Fsw_Deleted;
   fsw.Renamed += Fsw_Renamed;
   Read();
  }
  private static void Fsw_Renamed(object sender, RenamedEventArgs e)
  {
   Writeline ($"original name: {e.oldname} new name: {e.name}");
  }
  private static void Fsw_Deleted(object sender, FileSystemEventArgs e)
  {
   Writeline ($"{e.name} I was deleted");
  }
  private static void Fsw_Changed(object sender, FileSystemEventArgs e)
  {
   Writeline ($"{e.name} I was modified");
  }
  private static void Fsw_Created(object sender, FileSystemEventArgs e)
  {
   Writeline ($"{e.name} I was created");
  }
 }
}

summary

This article about the method of C # monitoring windows folder is introduced here. For more related C # monitoring windows folder content, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!