CreateFolder文件夹操作【Create】

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Web.UI;
7
8 namespace Plugins
9 {
10 public class CreateFolder:Page
11 {
12 /// <summary>
13 /// 创建文件夹
14 /// </summary>
15 /// <param name="strName">文件夹名称</param>
16 /// <returns></returns>
17 public bool createFolder(string strName)
18 {
19 string webDirectory = Server.MapPath("/");//获取网址根目录在服务器上的绝对路径
20 Directory.CreateDirectory(webDirectory+"\\"+strName);//创建用户目录
21 return CopyDir(webDirectory + "\\Photo", webDirectory + "\\" + strName);//将根目录中的Photo文件夹拷贝至用户文件夹
22 }
23
24 /// <summary>
25 /// 将整个文件夹复制到目标文件夹中。
26 /// </summary>
27 /// <param name="srcPath">源文件夹</param>
28 /// <param name="aimPath">目标文件夹</param>
29 /// <returns></returns>
30 public bool CopyDir(string srcPath, string aimPath)
31 {
32 try
33 {
34 // 检查目标目录是否以目录分割字符结束如果不是则添加之
35 if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
36 aimPath += Path.DirectorySeparatorChar;
37
38 // 判断目标目录是否存在如果不存在则新建之
39 if (!Directory.Exists(aimPath))
40 Directory.CreateDirectory(aimPath);
41
42 // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
43 // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
44
45 string[] fileList = Directory.GetFileSystemEntries(srcPath);
46
47
48 // 遍历所有的文件和目录
49 foreach (string file in fileList)
50 {
51 // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
52 if (Directory.Exists(file))
53 {
54 CopyDir(file, aimPath + Path.GetFileName(file));
55 }
56 // 否则直接Copy文件
57 else
58 {
59 File.Copy(file, aimPath + Path.GetFileName(file), true);
60 }
61 }
62
63 return true;
64 }
65 catch
66 {
67 return false;
68 }
69 }
70 }
71 }
原文地址:https://www.cnblogs.com/Archosaur/p/CreateFolder.html