FileSystemWatcher判断文件复制完成
来源:互联网 发布:k线图基础知识 知乎 编辑:程序博客网 时间:2024/06/05 03:09
引自:http://blog.csdn.net/dotnet90/article/details/21029625
使用 FileSystemWatcher 监视指定目录中的更改。可监视指定目录中的文件或子目录的更改。该组件可以监视本地计算机、网络驱动器或远程计算机上的文件。
可监视目录或文件中的若干种更改。例如,可监视文件或目录的 Attributes、LastWrite 日期和时间或 Size 方面的更改。通过设置FileSystemWatcher.NotifyFilter属性。
可监视文件或目录的重命名、删除或创建。例如,若要监视文本文件的重命名,请将 Filter 属性设置为“*.txt”。注意 公共文件系统操作可能会引发多个事件。例如,将文件从一个目录移到另一个目录时,可能会引发若干 OnChanged 以及一些 OnCreated 和 OnDelete事件。移动文件是一个包含多个简单操作的复杂操作,因此会引发多个事件。同样,一些应用程序(如反病毒软件)可能导致被 FileSystemWatcher 检测到的附加文件系统事件。
注意FileSystemWatcher.NotifyFilter 属性设置的监视和事件(OnChanged,OnCreated,OnDelete等事件)是and的关系。
例子程序:
public class Watcher
{
public static void Main()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if(args.Length != 2)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
//Suspend the calling thread until the file has been created
WaitForChangedResult cr= watcher.WaitForChanged(WatcherChangeTypes.Created);
// Wait for the user to quit the program.
Console.WriteLine("Press /'q/' to quit the sample.");
while(Console.Read()!='q');
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
但是,在实际使用过程中,由于需要监视的文件,必须在复制完成后才能进行后续工作,所以在监视时,需要不断进行判断,判断文件是否复制完成。所以修改
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + " " + e.ChangeType);
string filepath = e.FullPath; //mypath + "//" + cr.Name;
FileInfo fi = new FileInfo(filepath);
if (!fi.Exists)
{
Console.WriteLine("file not exits!!");
}
Console.WriteLine(fi.IsReadOnly.ToString());
//if (!fi.IsReadOnly)
//{
// fi.AppendText();
//}
HELLO: try
{
fi.OpenRead();
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
Thread.Sleep(3000);
goto HELLO;
}
OnRMChanged(e.FullPath);//对文件进行任意处理
}
最后等复制完成再对文件进行操作如下
private static void OnRMChanged(string mypath)
{
//对文件进行任意处理
}
根据该文章改造成我自己的文件监控器,哈哈- FileSystemWatcher判断文件复制完成
- FileSystemWatcher判断文件复制完成
- 使用FileSystemWatcher监测大文件时,如何判断文件已经创建完成?
- 使用 FileSystemWatcher 侦听文件
- FileSystemWatcher文件文件夹监听
- FileSystemWatcher文件监控
- 编译完成后复制文件
- 用FileSystemWatcher监视文件变化
- C# FileSystemWatcher监控指定文件
- 使用FileSystemWatcher监控文件变化
- powershell FileSystemWatcher监控文件变化
- C# FileSystemWatcher监控指定文件
- [问题] [悬赏完成]解密文件 [复制链接]
- java调用CMD完成文件复制
- 190_IO流_FileReader_Writer完成文件复制
- 利用FileReader和FileWriter完成文件复制
- 判断JS文件是否加载完成
- 文件監視類FileSystemWatcher 的用法總結
- Red hat安装详解
- 管理口令(P):[INS-30011] 输入的 ADMIN 口令不符合 Oracle 建议的标准
- eclipse下调试ROS工程
- github常用命令
- 百度地图 设置缩放等级
- FileSystemWatcher判断文件复制完成
- ObjC 类的加载和初始化(+load 和 +initialize 方法)
- linux stat函数讲解
- 多线程编程(一)FutureTask 测试类
- 【web】a标签点击时跳出确认框
- openssl编译安装-各种蛋疼
- centos7.2 源码安装kernel 4.8.1
- CSS 相对定位
- java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit(index_jsp.java:23)