當前位置:編程學習大全網 - 腳本源碼 - 如何復制壹個目錄裏面的所有目錄和文件

如何復制壹個目錄裏面的所有目錄和文件

下面介紹幾個我們在該例程中將要使用的類:1、Directory:Exposes static methods for creating, moving, and enumerating through directories and subdirectories.2、Path:Performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner.3、File:Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.這兩個類裏都是不可繼承的,是從object直接繼承來的,被實現成sealed,上面的解釋均來自MSDN。下面是實現的代碼,代碼中的細節不在這裏詳細描述,請看代碼註釋:// ======================================================// 實現壹個靜態方法將指定文件夾下面的所有內容copy到目標文件夾下面// ======================================================public static void CopyDir(string srcPath,string aimPath){// 檢查目標目錄是否以目錄分割字符結束如果不是則添加之if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)aimPath += Path.DirectorySeparatorChar;// 判斷目標目錄是否存在如果不存在則新建之if(!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);// 得到源目錄的文件列表,該裏面是包含文件以及目錄路徑的壹個數組// 如果妳指向copy目標文件下面的文件而不包含目錄請使用下面的方法// string[] fileList = Directory.GetFiles(srcPath);string[] fileList = Directory.GetFileSystemEntries(srcPath);// 遍歷所有的文件和目錄foreach(string file in fileList){// 先當作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件if(Directory.Exists(file))// 否則直接Copy文件elseFile.Copy(file,aimPath+Path.

  • 上一篇:如何統計app的安裝量?
  • 下一篇:為什麽很多手機廠商都模仿iphone?
  • copyright 2024編程學習大全網