本文介绍了使用FileSystemWatcher在文件系统上观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好..
我使用FileSystemWatcher在File System上观看。它可以在特定的文件夹或驱动器上观看。
对此有何想法?
Hello every one..
i use FileSystemWatcher for watch on File System. it can watch on particular folder or drive.
Any idea about this ?
推荐答案
public FileSystemWatcher fsw;
public void StartMonitoring()
{
//Get all Drives of Computer
DriveInfo[] dinfo = DriveInfo.GetDrives();
//Loop Through All The Drives
foreach (DriveInfo d in dinfo)
{
try
{
fsw = new FileSystemWatcher(d.RootDirectory.ToString());
fsw.EnableRaisingEvents = true;
fsw.IncludeSubdirectories = true;
fsw.Renamed += new RenamedEventHandler(FileRenamed);
fsw.Created += new FileSystemEventHandler(FileCreated);
fsw.Deleted += new FileSystemEventHandler(FileDeleted);
}
catch
{
}
}
}
public void FileRenamed(Object Sender, RenamedEventArgs e)
{
messagebox.show( e.FullPath, "Renamed");
}
public void FileCreated(Object Sender, FileSystemEventArgs e)
{
messagebox.show( e.FullPath, "Created");
}
public void FileDeleted(Object Sender, FileSystemEventArgs e)
{
messagebox.show( e.FullPath, "Deleted");
}
public void StopMonitoring()
{
fsw.Dispose();
}
这篇关于使用FileSystemWatcher在文件系统上观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!