0

基本的に、プログラムのログが保存される場所である 1 つのフォルダーを除いて、フォルダー内のすべてをコピーしたいと考えています。(ログを別の場所に保存できることはわかっていますが、理由があります)。これまでのところ、私は持っています:

private void btnCopyFiles_Click(object sender, EventArgs e)
    {
        try
        {
            //Copy all the files
            foreach (string newPath in Directory.GetFiles(@"\\xxx\yyy", "*.*",
                            SearchOption.AllDirectories)
                            .Where(p => p != Logging.fullLoggingPath))
                File.Copy(newPath, newPath.Replace(@"\\xxx\yyy", @"C:\bbb"));
            using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
            {
                Logging.Log("All necessary files copied to C:\\bbb", w);
            }
        }
        catch
        {
            using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
            {
                Logging.Log("Error copying files, make sure you have permissions to access the drive and write to the folder.", w);
            }
        }
    }

これを変更して、特定のフォルダーを除外するにはどうすればよいですか\\xxx\yyy

4

3 に答える 3

4
    foreach (string newPath in Directory.GetFiles(@"\\xxx\yyy", "*.*",
                    SearchOption.AllDirectories)
                    .Where(p=>p!=Logging.fullLoggingPath))
                File.Copy(newPath, newPath.Replace(@"\\xxx\yyy", @"C:\bbb"));
    using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
    {
         Logging.Log("All necessary files copied to C:\\bbb", w);
    }
于 2013-09-19T21:17:30.547 に答える
4

ループで、そのディレクトリを除外する条件を追加するだけです

If (Path.GetDirectory(newPath) == PathToExclude)
    Continue;
于 2013-09-19T21:18:51.130 に答える
3

フォルダの名前を確認してスキップします

foreach (string newPath in Directory.GetFiles(@"\\xxx\yyy", "*.*", SearchOption.AllDirectories))
{
    String currentFolder = "";
    int lastSlash = newPath.LastIndexOf("\\");
    int secondLastSlash = newPath.Substring(0, newPath.Length - (newPath.Length - lastSlash)).LastIndexOf("\\")+1;
        currentFolder = newPath.Substring(secondLastSlash,(lastSlash-secondLastSlash))

    If(!currentFolder.Equals(NameOfFolderNotToInclude))
        File.Copy(newPath, newPath.Replace(@"\\xxx\yyy", @"C:\bbb"), true);
}
        using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
        {
            Logging.Log("All necessary files copied to C:\\bbb", w);
        }

lastSlash 変数は、文字列内の最後の \ の位置であり、実際のファイルの後のテキストを示します。次に、ファイルのパスへの文字列の最後の \ 以降をすべて削除する部分文字列を作成します。これにより、フォルダーまでのパスが得られます。次に、部分文字列を使用して次の \ を見つけ、特定のフォルダーだけを抽出できるようにします。

したがって、C:\users\someone\desktop\test.txt などのパスでは、lastSlash は test.txt の前の \ を示します。これを使用して、C:\users\someone\desktop に等しい部分文字列を作成します。このパスの最後の \ を見つけるメソッドを使用して、すべてを組み合わせてデスクトップのフォルダーを抽出します。

よりユーザーフレンドリーで読みやすいので、Jim Mischel の提案を使用できます。以下を使用した例

foreach (string newPath in Directory.GetFiles(@"\\xxx\yyy", "*.*", SearchOption.AllDirectories))
{
    if(Path.GetDirectory(newPath) != PathToExclude)
        File.Copy(newPath, newPath.Replace(@"\\xxx\yyy", @"C:\bbb"), true);
}
        using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
        {
            Logging.Log("All necessary files copied to C:\\bbb", w);
        }
于 2013-09-19T21:16:36.133 に答える