最近做MVC網站時剛好用到,用以提供一個完整的文件夾并壓縮下載,正好做個筆記。
拷貝文件夾的所有內容到另一個文件夾內:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public static void CopyDir( string srcPath, string aimPath) { try { // 檢查目標目錄是否以目錄分割字符結束如果不是則添加之 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)) CopyDir(file, aimPath + Path.GetFileName(file)); // 否則直接Copy文件 else File.Copy(file, aimPath + Path.GetFileName(file), true ); } } catch { Console.WriteLine( "無法復制!" ); } } |
刪除文件夾:
1
|
Directory.Delete(path, true ); |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/lovecsharp094/p/5509295.html