检索文件目录信息

  有两个用来表示文件的类Directory和DirectoryInfo类。类似于表示文件的信息,Direcotry完全由静态方法组成:

     

 
  
1 /*
2 Example15_6.cs illustrates the Directory class
3   */
4
5 using System;
6 using System.IO;
7
8 class Example15_6
9 {
10
11 public static void Main()
12 {
13
14 // get the files from the root directory
15 string [] aFiles = Directory.GetFiles( " c:\\ " );
16
17 // and display them
18 foreach ( string s in aFiles)
19 Console.WriteLine(s);
20 }
21
22 }

DirectoryInfo类的使用示例如:

 
  
1 /*
2 Example15_7.cs illustrates the Directory class
3 */
4
5 using System;
6 using System.IO;
7
8 class Example15_7
9 {
10
11 // the DecipherAttributes method turns file attributes
12 // into something easier for people to read
13 public static void DecipherAttributes(FileAttributes f)
14 {
15 if ((f & FileAttributes.Archive) == FileAttributes.Archive)
16 Console.WriteLine( " Archive " );
17 if ((f & FileAttributes.Compressed) == FileAttributes.Compressed)
18 Console.WriteLine( " Compressed " );
19 if ((f & FileAttributes.Device) == FileAttributes.Device)
20 Console.WriteLine( " Device " );
21 if ((f & FileAttributes.Directory) == FileAttributes.Directory)
22 Console.WriteLine( " Directory " );
23 if ((f & FileAttributes.Encrypted) == FileAttributes.Encrypted)
24 Console.WriteLine( " Encrypted " );
25 if ((f & FileAttributes.Hidden) == FileAttributes.Hidden)
26 Console.WriteLine( " Hidden " );
27 if ((f & FileAttributes.NotContentIndexed) == FileAttributes.NotContentIndexed)
28 Console.WriteLine( " NotContentIndexed " );
29 if ((f & FileAttributes.Offline) == FileAttributes.Offline)
30 Console.WriteLine( " Offline " );
31 if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
32 Console.WriteLine( " ReadOnly " );
33 if ((f & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
34 Console.WriteLine( " ReparsePoint " );
35 if ((f & FileAttributes.SparseFile) == FileAttributes.SparseFile)
36 Console.WriteLine( " SparseFile " );
37 if ((f & FileAttributes.System) == FileAttributes.System)
38 Console.WriteLine( " System " );
39 if ((f & FileAttributes.Temporary) == FileAttributes.Temporary)
40 Console.WriteLine( " Temporary " );
41 }
42
43 public static void Main()
44 {
45
46 // create a DirectoryInfo object
47 DirectoryInfo di = new DirectoryInfo( " c:\\ " );
48
49 // retrieve and show the directory attributes
50 FileAttributes f = di.Attributes;
51 Console.WriteLine( " Directory c:\\ has attributes: " );
52 DecipherAttributes(f);
53
54 }
55
56 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值