簡體   English   中英

復制文件排除一個文件夾

[英]Copy files exclude one folder

基本上,我想復制一個文件夾(不包括一個文件夾)中的所有內容,該文件夾恰好是程序的日志存儲位置。 我知道我可以將日志存儲在其他地方,但是有我的理由 )。 到目前為止,我有:

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一個特定文件夾

    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);
    }

在循環中,只需添加條件即可排除該目錄

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

檢查文件夾的名稱並跳過它

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);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM