C#中如何创建文件夹,复制文件夹,删除文件夹的方法

转自:http://www.cnblogs.com/pegasus923/archive/2011/01/26/1944838.html

http://liuxiaoyong.iteye.com/blog/1164851

C#中对文件夹操作需要用到Directory Class。其中提供了创建、删除、移动、枚举等静态方法。该类不能被继承。

 

以下代码实现了创建文件夹。

?
1
2
3
4
if  (!Directory.Exists(sPath))
{
      Directory.CreateDirectory(sPath);
}

 

以下是MSDN上Directory Class的Sample code。

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

 

以下代码首先检查指定的文件夹是否存在,若存在则删除之,否则创建之。接下来移动文件夹,在其中创建文件并统计文件夹中文件数目。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using  System;
using  System.IO;
 
class  Test
{
     public  static  void  Main()
     {
         // Specify the directories you want to manipulate.
         string  path = @"c:\MyDir" ;
         string  target = @"c:\TestDir" ;
 
         try
         {
             // Determine whether the directory exists.
             if  (!Directory.Exists(path))
             {
                 // Create the directory it does not exist.
                 Directory.CreateDirectory(path);
             }
 
             if  (Directory.Exists(target))
             {
                 // Delete the target to ensure it is not there.
                 Directory.Delete(target, true );
             }
 
             // Move the directory.
             Directory.Move(path, target);
 
             // Create a file in the directory.
             File.CreateText(target + @"\myfile.txt" );
 
             // Count the files in the target directory.
             Console.WriteLine( "The number of files in {0} is {1}" ,
                 target, Directory.GetFiles(target).Length);
         }
         catch  (Exception e)
         {
             Console.WriteLine( "The process failed: {0}" , e.ToString());
         }
         finally  {}
     }
}

以下代码演示了如何计算文件夹大小。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// The following example calculates the size of a directory
// and its subdirectories, if any, and displays the total size
// in bytes.
 
using  System;
using  System.IO;
 
public  class  ShowDirSize
{
     public  static  long  DirSize(DirectoryInfo d)
     {   
         long  Size = 0;   
         // Add file sizes.
         FileInfo[] fis = d.GetFiles();
         foreach  (FileInfo fi in  fis)
         {     
             Size += fi.Length;   
         }
         // Add subdirectory sizes.
         DirectoryInfo[] dis = d.GetDirectories();
         foreach  (DirectoryInfo di in  dis)
         {
             Size += DirSize(di);  
         }
         return (Size); 
     }
     public  static  void  Main( string [] args)
     {
         if  (args.Length != 1)
         {
             Console.WriteLine( "You must provide a directory argument at the command line." );   
         }
         else
        
             DirectoryInfo d = new  DirectoryInfo(args[0]);
             long  dsize = DirSize(d);
             Console.WriteLine( "The size of {0} and its subdirectories is {1} bytes." , d, dsize);
         }
     }
}
 

删除文件夹: 

C#代码   收藏代码
    1. /// <summary>     
    2. /// C# 删除文件夹        
    3. /// </summary>     
    4. /// <param name="dir">删除的文件夹,全路径格式</param>     
    5.         private void DeleteFolder(string dir)  
    6.         {  
    7.             // 循环文件夹里面的内容     
    8.             foreach (string f in Directory.GetFileSystemEntries(dir))  
    9.             {  
    10.                 // 如果是文件存在     
    11.                 if (File.Exists(f))  
    12.                 {  
    13.                     FileInfo fi = new FileInfo(f);  
    14.                     if (fi.Attributes.ToString().IndexOf("Readonly") != 1)  
    15.                     {  
    16.                         fi.Attributes = FileAttributes.Normal;  
    17.                     }  
    18.                     // 直接删除其中的文件     
    19.                     File.Delete(f);  
    20.                 }  
    21.                 else  
    22.                 {  
    23.                     // 如果是文件夹存在     
    24.                     // 递归删除子文件夹     
    25.                     DeleteFolder(f);  
    26.                 }  
    27.             }  
    28.             // 删除已空文件夹     
    29.             Directory.Delete(dir);  
    30.         }  

复制文件夹: 

C#代码   收藏代码
    1. /// <summary>  
    2. /// 复制文件夹  
    3. /// </summary>  
    4. /// <param name="sourceFolder">待复制的文件夹</param>  
    5. /// <param name="destFolder">复制到的文件夹</param>  
    6.         private void CopyFolder(string sourceFolder, string destFolder)  
    7.         {  
    8.             if (!Directory.Exists(destFolder))  
    9.             {  
    10.                 Directory.CreateDirectory(destFolder);  
    11.             }  
    12.             string[] files = Directory.GetFiles(sourceFolder);  
    13.             foreach (string file in files)  
    14.             {  
    15.                 string name = Path.GetFileName(file);  
    16.   
    17.                 string dest = Path.Combine(destFolder, name);  
    18.   
    19.                 File.Copy(file, dest);  
    20.             }  
    21.             string[] folders = Directory.GetDirectories(sourceFolder);  
    22.             foreach (string folder in folders)  
    23.             {  
    24.                 string name = Path.GetFileName(folder);  
    25.   
    26.                 string dest = Path.Combine(destFolder, name);  
    27.   
    28.                 CopyFolder(folder, dest);  
    29.             }  
    30.         }  

转载于:https://www.cnblogs.com/jiangdd/archive/2012/10/15/2724454.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值