有两个用来表示文件的类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 }
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 }
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 }