C# - 复制文件 - 从文件夹到文件夹 - 只有新文件(如同步) - 一个想法

时间:2015-09-09 11:12:44

标签: c# file copy directory sync

有人可以帮我使用C#中的代码,我需要的是一个将文件从foder复制到文件夹的程序,如同步,但如果在第一个文件夹中删除它们,则不删除它们。例如,第一个文件夹是“source”文件夹,第二个文件夹是“destination”文件夹。

我的“来源”文件夹例如是来自iCloud的照片同步文件夹。那里有照片,我的程序将所有文件从它复制到目标文件夹。但是,如果我从源中删除照片或照片,则不同步目标文件夹。如果源中有新照片要复制到dest文件夹。我的想法是将我手机中的所有照片都放在destionation文件夹中。我不能在源文件夹中使用它们如果我从手机中删除照片它与文件夹同步然后从源文件夹中删除。而且我不想每次将新照片从文件夹复制到文件夹。只是一个小程序,检查是否有新的照片不在目标文件夹中,并将它们复制到它。

哦,这个简单的事情很多,但希望能帮助理解我想要的东西。第二件事是程序将如何检查源文件夹是否有变化?无论如何,希望有人有一个想法,也许代码的例子会很好。感谢。

最新来源:

DELETED

2 个答案:

答案 0 :(得分:3)

这里是更新的代码。我通过改变" Changed"中的事件处理程序做了一些小的修正。 to" Created&#34 ;,检查文件是否存在以及应用错误处理以在尝试访问文件之前检查该文件是否可用。

  private static void Main(string[] args)
    {
        watch();
        syncAllFilesFirstTime();
        Console.ReadLine();
    }

    private static void syncAllFilesFirstTime()
    {
        //Get list of files from sourcepath
        string[] arrFiles = Directory.GetFiles(strSourcePath);

        foreach (string sourceFiles in arrFiles)
        {
            //get filename
            string strFileName = Path.GetFileName(sourceFiles);
            string strDesFilePath = string.Format(@"{0}\{1}", strDesPath, strFileName);
            //check whether the destination path contatins the same file
            if (!File.Exists(strDesFilePath))
                File.Copy(sourceFiles, strDesFilePath, true);
        }
    }

    private static void syncUpdatedFiles(string strSourceFile)
    {
        //get filename
        string strFileName = Path.GetFileName(strSourceFile);
        string strDesFilePath = string.Format(@"{0}\{1}", strDesPath, strFileName);
        var val = File.Exists(strDesFilePath);
        //check whether the destination path contatins the same file
        if (!File.Exists(strDesFilePath))
        {
            for (; ;)
            {
                if (IsFileLocked(strSourceFile))
                {
                    File.Copy(strSourceFile, strDesFilePath, true);
                    break;
                }
            }
        }

    }

    private static void watch()
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = strSourcePath;
        //watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Filter = "*.*";
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
    }

    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
        syncUpdatedFiles(e.FullPath);
    }

    public static bool IsFileLocked(string strSourcePath)
    {
        try
        {
            using (Stream stream = new FileStream(strSourcePath, FileMode.Open))
            {
                return true;
            }
        }
        catch
        {
            return false;
        }
    }

答案 1 :(得分:1)

问题是目标(备份)中的文件已存在。

使用此版本的File.Copy(sourceFileName As String, destFileName As String, overwrite As Boolean)https://msdn.microsoft.com/en-us/library/9706cfs5(v=vs.110).aspx)并将参数覆盖为 true ,这样就可以了。您使用的版本已覆盖为false,因此当文件已存在时会抛出异常。

相关问题