复制文件排除一个文件夹

时间:2013-09-19 21:11:28

标签: c#

基本上我想复制文件夹中除一个文件夹之外的所有内容,这恰好是我的程序存储日志的地方。 (我知道我可以将日志存储在其他地方,但我有我的理由)。到目前为止,我有:

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

中的一个特定文件夹

3 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分:4)

在你的循环中,只需添加条件以排除该目录

If (Path.GetDirectory(newPath) == PathToExclude)
    Continue;

答案 2 :(得分: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);
        }