簡體   English   中英

將文件從子文件夾和主目錄復制到另一個位置C#

[英]Copying files from subfolders and main directory to another location c#

首先,我想說我喜歡這個網站。 這些年來,它一直非常有幫助。

當前,我在桌面C:\\Users\\Donjf\\Desktop\\New folder夾中有一個文件夾,在桌面上的該文件夾中,我有一個名為DVNSHP012的子文件夾或目錄,其中包含xml文件。 C:\\Users\\Donjf\\Desktop\\New folder包含xml文件。

我目前只能使用以下代碼從C:\\Users\\Donjf\\Desktop\\New folder的主目錄傳輸xml文件。

//Copy all the files & Replaces any files with the same name foreach (string newPath in Directory.GetFiles(DESKTOPLHA54U4, "*.xml", SearchOption.AllDirectories)) File.Copy(newPath, newPath.Replace(DESKTOPLHA54U4, path), true);

上面的代碼再次僅從C:\\Users\\Donjf\\Desktop\\New文件夾的主目錄傳輸,而不從包括C:\\Users\\Donjf\\Desktop\\New folder\\DVNSHP012的整個C:\\Users\\Donjf\\Desktop\\New folder\\DVNSHP012 我目前正在嘗試搜索所有子目錄,包括.xml文件的主目錄,然后將這些xml's文件傳輸到\\downloads\\

到目前為止,這是我嘗試過的。

if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            {
                TestWebMsgApp.WebMsgBox.Show("Folder created in root of drive");
            }
            foreach (string dirPath in Directory.GetDirectories(DESKTOPLHA54U4, "*", SearchOption.AllDirectories))
            {
                Directory.CreateDirectory(dirPath.Replace(DESKTOPLHA54U4, path));
                if (Directory.Exists(DESKTOPLHA54U4))
                {
                    //Copy all the files & Replaces any files with the same name
                    foreach (string newPath in Directory.GetFiles(DESKTOPLHA54U4, "*.xml",
                        SearchOption.AllDirectories))
                        File.Copy(newPath, newPath.Replace(DESKTOPLHA54U4, path), true);
                }
            }

我還嘗試過在整個Google和SO上進行搜索,然后再在此處發布,但似乎與我的特定問題無關。 有沒有辦法搜索所有* .xml文件,包括子目錄? 預先感謝您的任何幫助。

您的代碼應該可以正常工作,您只需要添加在目標文件夾中創建(子)目錄的代碼,因為File.Copy()不會創建它們(如果尚不存在)。 這對我有用:

var DESKTOPLHA54U4 = @"C:\Users\Donjf\Desktop\New folder\";
var path = @"C:\downloads\";

foreach (string newPath in Directory.GetFiles(DESKTOPLHA54U4, "*.xml", SearchOption.AllDirectories))
{
    // Ensure that target subdirectory exists
    Directory.CreateDirectory(Path.GetDirectoryName(newPath.Replace(DESKTOPLHA54U4, path)));

    File.Copy(newPath, newPath.Replace(DESKTOPLHA54U4, path), true);
}

另外,您可以創建遞歸復制目錄(和子目錄)的方法:

public static void CopyDirectory(string sourceDirectory, string destinationDirectory, string fileSearchPattern)
{
    // Ensures that (sub)target directory exists.
    Directory.CreateDirectory(destinationDirectory);

    foreach (string subDir in Directory.EnumerateDirectories(sourceDirectory))
    {
        // Recursively call CopyDirectory() for all subdirectories
        CopyDirectory(
            subDir,
            Path.Combine(destinationDirectory, Path.GetFileName(subDir)),
            fileSearchPattern);
    }

    foreach (string file in Directory.EnumerateFiles(sourceDirectory, fileSearchPattern, SearchOption.TopDirectoryOnly))
    {
        File.Copy(
            file,
            Path.Combine(destinationDirectory, Path.GetFileName(file)),
            true);
    }
}

並稱之為:

CopyDirectory(@"C:\Users\Donjf\Desktop\New folder\", @"C:\downloads\", "*.xml");

暫無
暫無

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

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