仅当源目录中不存在时才删除目标目录中的文件

时间:2016-12-04 14:27:34

标签: c# file copy backup

我正在尝试制作程序来备份我的文件。我已经复制部分工作但我想从目标目录中删除源目录中不存在的任何目录或文件。

我正在思考以下几点:

// Get existing files in destination
string[] existingTargetFiles = Directory.GetFiles(targetDir, "*.*", SearchOption.AllDirectories);

// Get existing directories in destination
    string[] existingTargetDirectories = Directory.GetDirectories(targetDir, "*", SearchOption.AllDirectories);

// Compare and delete directories that exist in destination but not source
foreach (string existingDirectory in existingTargetDirectories)
{
    if (!Directory.Exists(Path.Combine(sourceDir, existingDirectory)))
        Directory.Delete(Path.Combine(targetDir, existingDirectory));
    }
}

// Compare and delete files that exist in destination but not source
foreach (string existingFiles in existingTargetFiles)
{
    if (!Directory.Exists(Path.Combine(sourceDir, existingFiles)))
        Directory.Delete(Path.Combine(targetDir, existingFiles));
}

}

关于如何制作这样的作品的任何想法?

2 个答案:

答案 0 :(得分:1)

我使用以下在线发现的递归复制功能,该功能可复制所有文件,子目录。我创建了一个递归删除功能,以删除源目录中不存在的目标目录中的所有文件。这也会遍及所有子目录

Utilities.cs

public static void Copy(string sourceDirectory, string targetDirectory)
{
    DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
    DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

    CopyAll(diSource, diTarget);
}

private static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
    Directory.CreateDirectory(target.FullName);

    // Copy each file into the new directory.
    foreach (FileInfo fi in source.GetFiles())
    {
        var destinationFile = Path.Combine(target.FullName, fi.Name);
        fi.CopyTo(destinationFile, true);
    }

    // Copy each subdirectory using recursion.
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
    {
        DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
        CopyAll(diSourceSubDir, nextTargetSubDir);
    }
}

public static void Delete(string sourceDirectory, string targetDirectory)
{
    DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
    DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

    DeleteAll(diSource, diTarget);
}

private static void DeleteAll(DirectoryInfo source, DirectoryInfo target)
{
    if (!source.Exists)
    {
        target.Delete(true);
        return;
    }

    // Delete each existing file in target directory not existing in the source directory.
    foreach (FileInfo fi in target.GetFiles())
    {
        var sourceFile = Path.Combine(source.FullName, fi.Name);
        if (!File.Exists(sourceFile)) //Source file doesn't exist, delete target file
        {
            fi.Delete();
        }
    }

    // Delete non existing files in each subdirectory using recursion.
    foreach (DirectoryInfo diTargetSubDir in target.GetDirectories())
    {
        DirectoryInfo nextSourceSubDir = new DirectoryInfo(Path.Combine(source.FullName, diTargetSubDir.Name));
        DeleteAll(nextSourceSubDir, diTargetSubDir);
    }
}

您可以简单地将以上内容用作:

string SourcePath = @"C:\MySourceFolder\";
string PublishPath = @"C:\Published\";

Utilities.Copy(SourcePath, PublishPath);
Utilities.Delete(SourcePath, PublishPath);

答案 1 :(得分:0)

我做了一些更改以获取相对文件路径,然后将其与源目录结合使用,使用文件类进行文件删除等。

我首先将订单更改为文件删除,否则会导致找不到文件的一致性问题,因为在某些情况下文件夹被删除之前(因为您使用Directory.GetFiles(targetDir, "*.*", SearchOption.AllDirectories);递归获取文件列表)

        // Get existing files in destination
        string[] existingTargetFiles = Directory.GetFiles(targetDir, "*.*", SearchOption.AllDirectories);

        // Get existing directories in destination
        string[] existingTargetDirectories = Directory.GetDirectories(targetDir, "*", SearchOption.AllDirectories);

        // Compare and delete files that exist in destination but not source
        foreach (string existingFiles in existingTargetFiles)
        {

            if (!File.Exists(existingFiles.Replace(targetDir, sourceDir)))
            {

                File.Delete(existingFiles);
            }
        }
        // Compare and delete directories that exist in destination but not source
        foreach (string existingDirectory in existingTargetDirectories)
        {

            if (!Directory.Exists(existingDirectory.Replace(targetDir, sourceDir)))
            {
                Directory.Delete(existingDirectory,true);
            }
        }