当前位置:首页 > 码农资料 > 正文内容

C#监控文件变化情况

CCSSRW3年前 (2021-05-07)码农资料1861

文件被修改后实时读取,有两种办法,一种是使用定时器以固定时间读取文件内容变化,另一种是调用FileSystemWatcher实现避免无休止的读取;

string path="c:\test.txt";
private void FileWatcher(string path)
{
    FileSystemWatcher FSW = new FileSystemWatcher();
    FSW.Changed += new FileSystemEventHandler(File_Changed);
    FSW.Path = Path.GetDirectoryName(path);
    FSW.Filter = Path.GetFileName(path);
    FSW.EnableRaisingEvents = true;
}
private void File_Changed(object sender, System.IO.FileSystemEventArgs e)
{
    FileReader();
}


扫描二维码推送至手机访问

本文链接:http://xinrui.ren/post/69.html