文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C#中如何创建文件夹

C#中如何创建文件夹

时间:2011-01-26  来源:Pegasus923

 

if (!Directory.Exists(sPath))
                    {
                        Directory.CreateDirectory(sPath);
                    }

Directory Class

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

 

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

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

 

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

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

 

 

 

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载