C#监控文件变化情况
文件被修改后实时读取,有两种办法,一种是使用定时器以固定时间读取文件内容变化,另一种是调用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();
- }
扫描二维码推送至手机访问