1 class FileSysInfo 2 { 3 static void Main() 4 { 5 // You can also use System.Environment.GetLogicalDrives to 6 // obtain names of all logical drives on the computer. 7 System.IO.DriveInfo di = new System.IO.DriveInfo(@"C:"); 8 Console.WriteLine(di.TotalFreeSpace); 9 Console.WriteLine(di.VolumeLabel); 10 11 // Get the root directory and print out some information about it. 12 System.IO.DirectoryInfo dirInfo = di.RootDirectory; 13 Console.WriteLine(dirInfo.Attributes.ToString()); 14 15 // Get the files in the directory and print out some information about them. 16 System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*"); 17 18 19 foreach (System.IO.FileInfo fi in fileNames) 20 { 21 Console.WriteLine("{0}: {1}: {2}", fi.Name, fi.LastAccessTime, fi.Length); 22 } 23 24 // Get the subdirectories directly that is under the root. 25 // See "How to: Iterate Through a Directory Tree" for an example of how to 26 // iterate through an entire tree. 27 System.IO.DirectoryInfo[] dirInfos = dirInfo.GetDirectories("*.*"); 28 29 foreach (System.IO.DirectoryInfo d in dirInfos) 30 { 31 Console.WriteLine(d.Name); 32 } 33 34 // The Directory and File classes provide several static methods 35 // for accessing files and directories. 36 37 // Get the current application directory. 38 string currentDirName = System.IO.Directory.GetCurrentDirectory(); 39 Console.WriteLine(currentDirName); 40 41 // Get an array of file names as strings rather than FileInfo objects. 42 // Use this method when storage space is an issue, and when you might 43 // hold on to the file name reference for a while before you try to access 44 // the file. 45 string[] files = System.IO.Directory.GetFiles(currentDirName, "*.txt"); 46 47 foreach (string s in files) 48 { 49 // Create the FileInfo object only when needed to ensure 50 // the information is as current as possible. 51 System.IO.FileInfo fi = null; 52 try 53 { 54 fi = new System.IO.FileInfo(s); 55 } 56 catch (System.IO.FileNotFoundException e) 57 { 58 // To inform the user and continue is 59 // sufficient for this demonstration. 60 // Your application may require different behavior. 61 Console.WriteLine(e.Message); 62 continue; 63 } 64 Console.WriteLine("{0} : {1}",fi.Name, fi.Directory); 65 } 66 67 // Change the directory. In this case, first check to see 68 // whether it already exists, and create it if it does not. 69 // If this is not appropriate for your application, you can 70 // handle the System.IO.IOException that will be raised if the 71 // directory cannot be found. 72 if (!System.IO.Directory.Exists(@"C:UsersPublicTestFolder")) 73 { 74 System.IO.Directory.CreateDirectory(@"C:UsersPublicTestFolder"); 75 } 76 77 System.IO.Directory.SetCurrentDirectory(@"C:UsersPublicTestFolder"); 78 79 currentDirName = System.IO.Directory.GetCurrentDirectory(); 80 Console.WriteLine(currentDirName); 81 82 // Keep the console window open in debug mode. 83 Console.WriteLine("Press any key to exit."); 84 Console.ReadKey(); 85 } 86 }