C#文件只复制一次

时间:2013-09-11 07:49:52

标签: c#

使用我的filesystemwatcher,我检查何时需要将文件复制到第二个目录。每次。因此,例如我在dir1中放入test.txt,它会自动将文件复制或剪切到dir2。现在,当我在dir1中输入test2.txt时,没有任何反应。为什么第二次没有发生任何事情?

编辑:这是 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();
}
}

0 个答案:

没有答案