码迷,mamicode.com
首页 > Windows程序 > 详细

C# 目录(文件夹)复制实现

时间:2015-07-10 11:09:40      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

 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         }

 

C# 目录(文件夹)复制实现

标签:

原文地址:http://www.cnblogs.com/seayxu/p/4635059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!