C#實現複製檔案夾中檔案到另一個檔案夾的方法

來源:互聯網
上載者:User

標籤:rcp   cep   處理   char   foreach   檢查   複製   數組   路徑   

private void CopyDir(string srcPath, string aimPath)
{
try
{
// 檢查目標目錄是否以目錄分割字元結束如果不是則添加
if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
{
aimPath += System.IO.Path.DirectorySeparatorChar;
}
// 判斷目標目錄是否存在如果不存在則建立
if (!System.IO.Directory.Exists(aimPath))
{
System.IO.Directory.CreateDirectory(aimPath);
}
// 得到來源目錄的檔案清單,該裡面是包含檔案以及目錄路徑的一個數組
// 如果你指向copy目標檔案下面的檔案而不包含目錄請使用下面的方法
// string[] fileList = Directory.GetFiles(srcPath);
string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
// 遍曆所有的檔案和目錄
foreach (string file in fileList)
{
// 先當作目錄處理如果存在這個目錄就遞迴Copy該目錄下面的檔案
if (System.IO.Directory.Exists(file))
{
CopyDir(file, aimPath + System.IO.Path.GetFileName(file));
}
// 否則直接Copy檔案
else
{
System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true);
}
}
}
catch (Exception e)
{
throw;
}
}

C#實現複製檔案夾中檔案到另一個檔案夾的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.