使用我的 filesystemwatcher,我检查何时需要将文件复制到第二个目录。每次。因此,例如,我将 test.txt 放入 dir1 中,它会自动将文件复制或剪切到 dir2。现在,当我将 test2.txt 放入 dir1 时,什么也没有发生。为什么第二次什么都没有发生?
编辑:这是一个Windows 服务
这是我的代码:
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
variable_reset();
cut_copy = ConfigurationManager.AppSettings[@"cut"];
logger("File created> " + e.FullPath + " -Date:" + DateTime.Now);
filepath = Path.Combine(source, e.Name);
name = Path.GetFileNameWithoutExtension(filepath);
extension = Path.GetExtension(e.FullPath);
size = e.Name.Length;
strSelectCmd = "INSERT INTO files (name,size,last_edit,extension) VALUES('" + name + "','" + size + "',now(),'" + extension + "')";
readquery = "select * from files where name='" + name + "'";
Mysql();
postgresql();
Record();
if (string.IsNullOrEmpty(filename) == false)
{
if (Directory.Exists(e.FullPath))
{
copyfolder();
Directory.CreateDirectory(target);
}
else
{
if (WaitForFileAvailable(e.FullPath, TimeSpan.FromSeconds(10)))
{
var file = Path.Combine(source, e.Name);
var copy_file = Path.Combine(target, e.Name);
var destination = Path.Combine(target, Path.ChangeExtension(source, Path.GetExtension(source)));
if ((String.Compare(cut_copy, "cut"))==0)
{
if (File.Exists(file))// Check to see if the file exists.
{ //If it does delete the file in the target and copy the one from the source to the target.
File.Delete(copy_file);
}
File.Move(e.FullPath, Path.Combine(target, e.Name));
}
if ((String.Compare(cut_copy, "copy"))==0)
{
if (File.Exists(file))// Check to see if the file exists.
{ //If it does delete the file in the target and copy the one from the source to the target.
File.Delete(copy_file);
}
File.Copy(e.FullPath, Path.Combine(target, e.Name));
}
}
else // The file failed to become available within 10 seconds.
{
logger("Copy has failed reason: File is being used by another program");
}
}
}
else
{
query = "INSERT INTO files (name,size,last_edit,extension) VALUES('" + name + "','" + size + "',now(),'" + extension + "')";
Mysql();
}
}