Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#文件只复制一次_C# - Fatal编程技术网

C#文件只复制一次

C#文件只复制一次,c#,C#,我用filesystemwatcher检查文件何时需要复制到第二个目录。每次都是。例如,我将test.txt放在dir1中,它会自动将文件复制或剪切到dir2。现在,当我在dir1中输入test2.txt时,什么都没有发生。为什么第二次什么都没发生 编辑:这是一项Windows服务 这是我的密码: private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e) { variab

我用filesystemwatcher检查文件何时需要复制到第二个目录。每次都是。例如,我将test.txt放在dir1中,它会自动将文件复制或剪切到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();
}
}

我们真的不需要查看事件处理程序的整个主体(
fileSystemWatcher1\u创建的
)-一个简单的
MessageBox.Show(…)
就足够了。也许这是您需要帮助缩小问题所在的简单示例。什么是
source
?你把它放在哪里?参见code
var file=Path.Combine(源代码,e.Name)@JonathonReinhart这是一项服务……在这种情况下,“什么都没发生”是什么意思?该方法没有被调用吗?它有例外吗?它是否输入
if
案例?电视没有打开吗?@loko在Win Forms应用程序中编写了一个简单的示例,并使其核心正常工作。你必须学会缩小问题的范围。