原创 C# 文件夹操作

2009-11-9 18:38 2665 7 7 分类: 软件与OS
  1. 追加文件   
  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. string OrignFile,NewFile;   
  10. OrignFile = Server.MapPath(".")+"\\myText.txt";   
  11. NewFile = Server.MapPath(".")+"\\myTextCopy.txt";   
  12. File.Copy(OrignFile,NewFile,true);  
  13.   
  14. 删除文件   
  15. string delFile = Server.MapPath(".")+"\\myTextCopy.txt";   
  16. File.Delete(delFile);  
  17.   
  18. 移动文件   
  19. string OrignFile,NewFile;   
  20. OrignFile = Server.MapPath(".")+"\\myText.txt";   
  21. NewFile = Server.MapPath(".")+"\\myTextCopy.txt";   
  22. File.Move(OrignFile,NewFile);  
  23.   
  24. 创建目录   
  25. // 创建目录c:\sixAge   
  26. DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge");   
  27. // d1指向c:\sixAge\sixAge1   
  28. DirectoryInfo d1=d.CreateSubdirectory("sixAge1");   
  29. // d2指向c:\sixAge\sixAge1\sixAge1_1   
  30. DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");   
  31. // 将当前目录设为c:\sixAge   
  32. Directory.SetCurrentDirectory("c:\\sixAge");   
  33. // 创建目录c:\sixAge\sixAge2   
  34. Directory.CreateDirectory("sixAge2");   
  35. // 创建目录c:\sixAge\sixAge2\sixAge2_1   
  36. Directory.CreateDirectory("sixAge2\\sixAge2_1");  
  37.   
  38. 递归删除文件夹及文件   
  39. <%@ Page Language=C#%>   
  40. <%@ Import namespace="System.IO"%>   
  41. <Script runat=server>   
  42. public void DeleteFolder(string dir)   
  43. {   
  44.     if (Directory.Exists(dir)) //如果存在这个文件夹删除之   
  45.     {   
  46.         foreach(string d in Directory.GetFileSystemEntries(dir))   
  47.         {   
  48.             if(File.Exists(d))   
  49.                 File.Delete(d); //直接删除其中的文件   
  50.             else   
  51.                 DeleteFolder(d); //递归删除子文件夹   
  52.         }   
  53.         Directory.Delete(dir); //删除已空文件夹   
  54.         Response.Write(dir+" 文件夹删除成功");   
  55.     }   
  56.     else   
  57.         Response.Write(dir+" 该文件夹不存在"); //如果文件夹不存在则提示   
  58. }  
  59.   
  60. protected void Page_Load (Object sender ,EventArgs e)   
  61. {   
  62.     string Dir="D:\\gbook\\11";   
  63.     DeleteFolder(Dir); //调用函数删除文件夹   
  64. }  
  65.   
  66.   
  67. // ======================================================  
  68. // 实现一个静态方法将指定文件夹下面的所有内容copy到目标文件夹下面  
  69. // 如果目标文件夹为只读属性就会报错。  
  70. // April 18April2005 In STU  
  71. // ======================================================  
  72. public static void CopyDir(string srcPath,string aimPath)  
  73. {  
  74.    try  
  75.    {  
  76.     // 检查目标目录是否以目录分割字符结束如果不是则添加之  
  77.     if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)   
  78.      aimPath += Path.DirectorySeparatorChar;  
  79.     // 判断目标目录是否存在如果不存在则新建之  
  80.     if(!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);  
  81.     // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组  
  82.     // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法  
  83.     // string[] fileList = Directory.GetFiles(srcPath);  
  84.     string[] fileList = Directory.GetFileSystemEntries(srcPath);  
  85.     // 遍历所有的文件和目录  
  86.     foreach(string file in fileList)  
  87.     {  
  88.      // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件  
  89.      if(Directory.Exists(file))  
  90.       CopyDir(file,aimPath+Path.GetFileName(file));  
  91.       // 否则直接Copy文件  
  92.      else  
  93.       File.Copy(file,aimPath+Path.GetFileName(file),true);  
  94.     }  
  95.    }  
  96.    catch (Exception e)  
  97.    {  
  98.     MessageBox.Show (e.ToString());  
  99.    }  
  100. }  
  101.   
  102. // ======================================================  
  103. // 实现一个静态方法将指定文件夹下面的所有内容Detele  
  104. // 测试的时候要小心操作,删除之后无法恢复。  
  105. // April 18April2005 In STU  
  106. // ======================================================  
  107. public static void DeleteDir(string aimPath)  
  108. {  
  109.    try  
  110.    {  
  111.     // 检查目标目录是否以目录分割字符结束如果不是则添加之  
  112.     if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)   
  113.      aimPath += Path.DirectorySeparatorChar;  
  114.     // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组  
  115.     // 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法  
  116.     // string[] fileList = Directory.GetFiles(aimPath);  
  117.     string[] fileList = Directory.GetFileSystemEntries(aimPath);  
  118.     // 遍历所有的文件和目录  
  119.     foreach(string file in fileList)  
  120.     {  
  121.      // 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件  
  122.      if(Directory.Exists(file))  
  123.      {  
  124.       DeleteDir(aimPath+Path.GetFileName(file));  
  125.      }  
  126.       // 否则直接Delete文件  
  127.      else  
  128.      {  
  129.       File.Delete (aimPath+Path.GetFileName(file));  
  130.      }  
  131.     }  
  132.     //删除文件夹  
  133.     System.IO .Directory .Delete (aimPath,true);  
  134.    }  
  135.    catch (Exception e)  
  136.    {  
  137.     MessageBox.Show (e.ToString());  
  138.    }  
  139. }  
  140.   
  141. 需要引用命名空间:  
  142. using System.IO;  
  143.   
  144. /**//// <summary>  
  145. /// 拷贝文件夹(包括子文件夹)到指定文件夹下,源文件夹和目标文件夹均需绝对路径. 格式: CopyFolder(源文件夹,目标文件夹);  
  146. /// </summary>  
  147. /// <param name="strFromPath"></param>  
  148. /// <param name="strToPath"></param>  
  149.   
  150. //--------------------------------------------------  
  151. //作者:明天去要饭 QQ:305725744  
  152. //---------------------------------------------------  
  153.   
  154. public static void CopyFolder(string strFromPath,string strToPath)  
  155. {  
  156.    //如果源文件夹不存在,则创建  
  157.    if (!Directory.Exists(strFromPath))  
  158.    {      
  159.     Directory.CreateDirectory(strFromPath);  
  160.    }    
  161.   
  162.    //取得要拷贝的文件夹名  
  163.    string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf("\\") + 1,strFromPath.Length - strFromPath.LastIndexOf("\\") - 1);    
  164.   
  165.    //如果目标文件夹中没有源文件夹则在目标文件夹中创建源文件夹  
  166.    if (!Directory.Exists(strToPath + "\\" + strFolderName))  
  167.    {      
  168.     Directory.CreateDirectory(strToPath + "\\" + strFolderName);  
  169.    }  
  170.    //创建数组保存源文件夹下的文件名  
  171.    string[] strFiles = Directory.GetFiles(strFromPath);  
  172.   
  173.    //循环拷贝文件  
  174.    for(int i = 0;i < strFiles.Length;i++)  
  175.    {  
  176.     //取得拷贝的文件名,只取文件名,地址截掉。  
  177.     string strFileName = strFiles.Substring(strFiles.LastIndexOf("\\") + 1,strFiles.Length - strFiles.LastIndexOf("\\") - 1);  
  178.     //开始拷贝文件,true表示覆盖同名文件  
  179.     File.Copy(strFiles,strToPath + "\\" + strFolderName + "\\" + strFileName,true);  
  180.    }  
  181.   
  182.    //创建DirectoryInfo实例  
  183.    DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);  
  184.    //取得源文件夹下的所有子文件夹名称  
  185.    DirectoryInfo[] ZiPath = dirInfo.GetDirectories();  
  186.    for (int j = 0;j < ZiPath.Length;j++)  
  187.    {  
  188.     //获取所有子文件夹名  
  189.     string strZiPath = strFromPath + "\\" + ZiPath[j].ToString();     
  190.     //把得到的子文件夹当成新的源文件夹,从头开始新一轮的拷贝  
  191.     CopyFolder(strZiPath,strToPath + "\\" + strFolderName);  
  192.    }  
  193. }
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
7
关闭 站长推荐上一条 /3 下一条