C # Windows folder monitoring

Do you see the child is not in the picture stored on the computer and worry about whether you are unable to monitor employees stored on computer stuff to worry about, then today you recommend this product is definitely your best choice, it is produced by US manufacturers, in full compliance with international standards of products, the perfect support for more than 98 systems Windows, he is FileSystemWatcher cards monitor. He listens for file system change notifications and raises events when a directory or directory file changes. Here we take a look at his details.

Zero, minutiae

  1. Commonly used methods are:
  • OnChanged (FileSystemEventArgs)
    when changes are monitoring the size of files in a directory or directories, system attributes, last write time, last access time, or security permissions when calls this event.
  • OnCreated (FileSystemEventArgs)
    will call this event when you create a file or directory in the directory being monitored. It should be noted that while copying and moving is not created, but it will also trigger this event. If you copy or move a file to be monitored directory, it will immediately trigger OnCreated event, followed by one or more OnChanged events.
  • OnDeleted (FileSystemEventArgs)
    penalties this event When you delete a monitored directory file or directory. When we cut from the monitored directory file out, it will trigger this event.
  • OnRenamed (RenamedEventArgs)
    calls this method when you rename the directory to be monitored file or directory.
  1. Common attributes are:
  • Filter: Gets or sets the filter string for determining which files in the directory to be monitored.
  • IncludeSubdirectories: Gets or sets whether the monitor monitored subdirectory of the directory.
  • InternalBufferSize: internal buffer size, up to 64K, the default is 8K.
  • The NotifyFilter: After obtaining or setting what changes need to be captured, the type can be set as follows:
Types of Explanation
Attributes Properties file or folder
CreationTime File or folder creation time
DirectoryName Directory name
FileName file name
LastAccess The last access time
LastWrite Last Modified
Security Security Settings
Size size
  • path: the need to monitor the folder path

First, an example

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($"原名:{e.OldName}   新名称:{e.Name}");
        }

        private static void Fsw_Deleted(object sender, FileSystemEventArgs e)
        {
            WriteLine($"{e.Name}  我被删除了");
        }

        private static void Fsw_Changed(object sender, FileSystemEventArgs e)
        {
            WriteLine($"{e.Name}  我被修改了");
        }

        private static void Fsw_Created(object sender, FileSystemEventArgs e)
        {
            WriteLine($"{e.Name}  我被创建了");
        }
    }
}

Published 204 original articles · won praise 101 · Views 350,000 +

Guess you like

Origin blog.csdn.net/gangzhucoll/article/details/103792499