C#对于文件操作

 1 //C#追加文件 
 2 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\myText.txt"); 
 3 sw.WriteLine("追逐理想"); 
 4 sw.WriteLine("kzlll"); 
 5 sw.WriteLine(".NET笔记"); 
 6 sw.Flush(); 
 7 sw.Close(); 
 8 
 9 //C#拷贝文件 
10 string OrignFile,NewFile; 
11 OrignFile = Server.MapPath(".")+"\myText.txt"; 
12 NewFile = Server.MapPath(".")+"\myTextCopy.txt"; 
13 File.Copy(OrignFile,NewFile,true); 
14 
15 //C#删除文件 
16 string delFile = Server.MapPath(".")+"\myTextCopy.txt"; 
17 File.Delete(delFile); 
18 
19 //C#移动文件 
20 string OrignFile,NewFile; 
21 OrignFile = Server.MapPath(".")+"\myText.txt"; 
22 NewFile = Server.MapPath(".")+"\myTextCopy.txt"; 
23 File.Move(OrignFile,NewFile); 
24 
25 //C#创建目录 
26 // 创建目录c:sixAge 
27 DirectoryInfo d=Directory.CreateDirectory("c:\sixAge"); 
28 // d1指向c:sixAgesixAge1 
29 DirectoryInfo d1=d.CreateSubdirectory("sixAge1"); 
30 // d2指向c:sixAgesixAge1sixAge1_1 
31 DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1"); 
32 // 将当前目录设为c:sixAge 
33 Directory.SetCurrentDirectory("c:\sixAge"); 
34 // 创建目录c:sixAgesixAge2 
35 Directory.CreateDirectory("sixAge2"); 
36 // 创建目录c:sixAgesixAge2sixAge2_1 
37 Directory.CreateDirectory("sixAge2\sixAge2_1"); 
38 
39 //递归删除文件夹及文件 
40 <%@ Page Language=C#%> 
41 <%@ Import namespace="System.IO"%> 
42 <Script runat=server> 
43 public void DeleteFolder(string dir) 
44 { 
45     if (Directory.Exists(dir)) //如果存在这个文件夹删除之 
46     { 
47         foreach(string d in Directory.GetFileSystemEntries(dir)) 
48         { 
49             if(File.Exists(d)) 
50                 File.Delete(d); //直接删除其中的文件 
51             else 
52                 DeleteFolder(d); //递归删除子文件夹 
53         } 
54         Directory.Delete(dir); //删除已空文件夹 
55         Response.Write(dir+" 文件夹删除成功"); 
56     } 
57     else 
58         Response.Write(dir+" 该文件夹不存在"); //如果文件夹不存在则提示 
59 } 
60 
61 protected void Page_Load (Object sender ,EventArgs e) 
62 { 
63     string Dir="D:\gbook\11"; 
64     DeleteFolder(Dir); //调用函数删除文件夹 
65 } 

 // ======================================================
  // 实现一个静态方法将指定文件夹下面的所有内容copy到目标文件夹下面
  // 如果目标文件夹为只读属性就会报错。
 // ======================================================

 1 public static void CopyDir(string srcPath,string aimPath)
 2 {
 3     try
 4     {
 5         // 检查目标目录是否以目录分割字符结束如果不是则添加之
 6         if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar) 
 7             aimPath += Path.DirectorySeparatorChar;
 8         // 判断目标目录是否存在如果不存在则新建之
 9         if(!Directory.Exists(aimPath)) 
10             Directory.CreateDirectory(aimPath);
11         // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
12         // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
13         // string[] fileList = Directory.GetFiles(srcPath);
14         string[] fileList = Directory.GetFileSystemEntries(srcPath);
15         // 遍历所有的文件和目录
16         foreach(string file in fileList)
17         {
18             // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
19             if(Directory.Exists(file))
20                 CopyDir(file,aimPath+Path.GetFileName(file));
21             // 否则直接Copy文件
22             else
23                 File.Copy(file,aimPath+Path.GetFileName(file),true);
24         }
25     }
26     catch (Exception e)
27     {
28         MessageBox.Show (e.ToString());
29     }
30 }
31 
32 
33 public static void DeleteDir(string aimPath)
34 {
35     try
36     {
37         // 检查目标目录是否以目录分割字符结束如果不是则添加之
38         if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar) 
39             aimPath += Path.DirectorySeparatorChar;
40         // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
41         // 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法
42         // string[] fileList = Directory.GetFiles(aimPath);
43         string[] fileList = Directory.GetFileSystemEntries(aimPath);
44         // 遍历所有的文件和目录
45         foreach(string file in fileList)
46         {
47             // 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件
48             if(Directory.Exists(file))
49             {
50                 DeleteDir(aimPath+Path.GetFileName(file));
51             }
52             // 否则直接Delete文件
53             else
54             {
55                 File.Delete (aimPath+Path.GetFileName(file));
56             }
57         }
58         //删除文件夹
59         System.IO .Directory .Delete (aimPath,true);
60     }
61     catch (Exception e)
62     {
63         MessageBox.Show (e.ToString());
64     }
65 }
原文地址:https://www.cnblogs.com/JLZT1223/p/6148798.html