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

C# 目錄(文件夾)復制實現,

編輯:C#入門知識

C# 目錄(文件夾)復制實現,


 1         private static void CopyDir(DirectoryInfo origin, string target)
 2         {
 3             if (!target.EndsWith("\\"))
 4             {
 5                 target += "\\";
 6             }
 7             if (!Exist(target))
 8             {
 9                 Create(target);
10                 //Create(target, origin.GetAccessControl());
11             }
12             FileInfo[] fileList = origin.GetFiles();
13             DirectoryInfo[] dirList = origin.GetDirectories();
14             foreach (FileInfo fi in fileList)
15             {
16                 File.Copy(fi.FullName, target + fi.Name, true);
17             }
18             foreach (DirectoryInfo di in dirList)
19             {
20                 CopyDir(di, target + di.Name);
21             }
22             DirectoryInfo tmp = new DirectoryInfo(target);
23             tmp.Attributes = origin.Attributes;
24             tmp.SetAccessControl(origin.GetAccessControl());
25         }
26 
27         private static void CopyDir(string origin, string target)
28         {
29             if (!origin.EndsWith("\\"))
30             {
31                 origin += "\\";
32             }
33             if (!target.EndsWith("\\"))
34             {
35                 target += "\\";
36             }
37 
38             DirectoryInfo info = new DirectoryInfo(origin);
39             if (!Exist(target))
40             {
41                 Create(target,info.GetAccessControl());//創建目錄,訪問權限
42             }
43 
44             FileInfo[] fileList = info.GetFiles();
45             DirectoryInfo[] dirList = info.GetDirectories();
46             foreach (FileInfo fi in fileList)
47             {
48                 File.Copy(fi.FullName, target + fi.Name, true);
49             }
50             foreach (DirectoryInfo di in dirList)
51             {
52                 //CopyDir(origin + "\\" + di.Name, target + "\\" + di.Name);
53                 CopyDir(di.FullName, target + "\\" + di.Name);
54             }
55             //設置目錄屬性和訪問權限
56             DirectoryInfo tmp = new DirectoryInfo(target);
57             tmp.Attributes = info.Attributes;
58             tmp.SetAccessControl(info.GetAccessControl());
59         }

 

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