C#文件只复制一次
我用filesystemwatcher检查文件何时需要复制到第二个目录。每次都是。例如,我将test.txt放在dir1中,它会自动将文件复制或剪切到dir2。现在,当我在dir1中输入test2.txt时,什么都没有发生。为什么第二次什么都没发生 编辑:这是一项Windows服务 这是我的密码:C#文件只复制一次,c#,C#,我用filesystemwatcher检查文件何时需要复制到第二个目录。每次都是。例如,我将test.txt放在dir1中,它会自动将文件复制或剪切到dir2。现在,当我在dir1中输入test2.txt时,什么都没有发生。为什么第二次什么都没发生 编辑:这是一项Windows服务 这是我的密码: private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e) { variab
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();
}
}
我们真的不需要查看事件处理程序的整个主体(
fileSystemWatcher1\u创建的
)-一个简单的MessageBox.Show(…)
就足够了。也许这是您需要帮助缩小问题所在的简单示例。什么是source
?你把它放在哪里?参见codevar file=Path.Combine(源代码,e.Name)代码>@JonathonReinhart这是一项服务……在这种情况下,“什么都没发生”是什么意思?该方法没有被调用吗?它有例外吗?它是否输入if
案例?电视没有打开吗?@loko在Win Forms应用程序中编写了一个简单的示例,并使其核心正常工作。你必须学会缩小问题的范围。