3

私はしばらくの間、この問題にかなり悩まされています。Folder1\directory1 から Updated\directory1 にすべてをコピー (更新) して同じファイルを上書きする必要がありますが、Updated\directory1 に既に存在するが Folder1\directory1 には存在しないファイルは削除しません。私の質問をより明確にするために、これは私の予想される結果です:

C:\フォルダ1\ディレクトリ1

サブフォルダー1

subtext1.txt (2KB)

サブフォルダー2

名前.txt (2KB)

C:\更新済み\ディレクトリ1

サブフォルダー1

subtext1.txt (1KB)

subtext2.txt (2KB)

期待される結果:

C:\更新済み\ディレクトリ1

サブフォルダー1

subtext1.txt (2KB) <--- 更新

subtext2.txt (2KB)

subfolder2 <--- 追加

name.txt (2KB) <--- 追加

現在使用してDirectory.Move(source, destination)いますが、宛先フォルダーの一部が存在しないため、宛先部分に問題があります。私の唯一のアイデアは、String.Trim追加のフォルダーがあるかどうかを判断するために使用することですが、ディレクトリは動的であると想定されているため、実際には使用できません (サブディレクトリまたはフォルダーが増える可能性があります)。私は本当に立ち往生しています。私のものを動かすためのヒントやコードをお勧めできますか? ありがとう!

4

4 に答える 4

3

msdn http://msdn.microsoft.com/en-us/library/cc148994.aspxからこの例を入手しました。これはあなたが探しているものだと思います

// To copy all the files in one directory to another directory. 
    // Get the files in the source folder. (To recursively iterate through 
    // all subfolders under the current directory, see 
    // "How to: Iterate Through a Directory Tree.")
    // Note: Check for target path was performed previously 
    //       in this code example. 
    if (System.IO.Directory.Exists(sourcePath))
    {
        string[] files = System.IO.Directory.GetFiles(sourcePath);

        // Copy the files and overwrite destination files if they already exist. 
        foreach (string s in files)
        {
            // Use static Path methods to extract only the file name from the path.
            fileName = System.IO.Path.GetFileName(s);
            destFile = System.IO.Path.Combine(targetPath, fileName);
            System.IO.File.Copy(s, destFile, true);
        }
    }
    else
    {
        Console.WriteLine("Source path does not exist!");
    }

存在しないフォルダー パスを処理する必要がある場合は、新しいフォルダーを作成する必要があります。

if (System.IO.Directory.Exists(targetPath){
    System.IO.Directory.CreateDirectory(targetPath);
}
于 2013-09-25T04:53:15.710 に答える