程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 把一個文件夾下所有文件復制到另一個文件夾下,

C# 把一個文件夾下所有文件復制到另一個文件夾下,

編輯:C#入門知識

C# 把一個文件夾下所有文件復制到另一個文件夾下,


public static void CopyDirectory(string srcPath, string destPath)
{
  try
    {
    DirectoryInfo dir = new DirectoryInfo(srcPath);
    FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //獲取目錄下(不包含子目錄)的文件和子目錄     foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判斷是否文件夾 { if (!Directory.Exists(destPath+"\\"+i.Name)) { Directory.CreateDirectory(destPath + "\\" + i.Name); //目標目錄下不存在此文件夾即創建子文件夾 } CopyDir(i.FullName, destPath + "\\" + i.Name); //遞歸調用復制子文件夾 } else { File.Copy(i.FullName, destPath + "\\" + i.Name,true); //不是文件夾即復制文件,true表示可以覆蓋同名文件 } } } catch (Exception e) { throw; }
}

 

調用CopyDirectory方法前可以先判斷原路徑與目標路徑是否存在


if(Directory.Exists(srcPath)&&Directory.Exists(destPath)) { CopyDirectory(srcPath,destPath);
}

 原文地址:http://www.cnblogs.com/iamlucky/p/5996222.html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved