在.Net 1.1中,要获得磁盘信息,只有通过Win32的API来获得,例如:

None.gif//获取磁盘剩余空间;
None.gif
[DllImport( "kernel32.dll", EntryPoint="GetDiskFreeSpaceA" )]
None.gif
public static extern int GetDiskFreeSpace(string lpRootPathName,ref int lpSectorsPerCluster,
None.gif                                                                                                           
ref int lpBytesPerSector,
None.gif                                                                                                           
ref int lpNumberOfFreeClusters,
None.gif                                                                                                           
ref int lpTotalNumberOfClusters);
None.gif
None.gif
//获取磁盘类型;
None.gif
[DllImport( "kernel32.dll", EntryPoint="GetDriveTypeA" )]
None.gif
public static extern int GetDriveType(string nDrive);
None.gif

然而在.Net2.0中,不需要做这些烦人的工作,它已经将这些Win32的API放到了Framework的类库中。在命名空间System.IO下有DriveInfo类,该类分别包括属性:TotalSize,TotalFresSpace,AvailableFreeSpace,DriveFormat,DriveType,VolumeLabel等属性。现在要获得有关磁盘的信息,就非常容易了。

None.gifusing System.IO;
None.gif
None.gif
string driveName = "C:\\";
None.gifDriveInfo driveInfo 
= new DriveInfo(driveName);
None.gif
None.gifConsole.WriteLine(
"The volume name is {0}",driveInfo.VolumeLabel);
None.gifConsole.WriteLine(
"The total space is {0}",driveInfo.TotalSize);
None.gifConsole.WriteLine(
"The free space is {0}",driveInfo.TotalFreeSpace);

另外,DriveInfo类还有一个静态方法GetDrives(),它能获得当前计算机所有驱动器的信息:

None.gifDriveInfo[] drives = DriveInfo.GetDrives();

不知道.Net2.0又封装了多少.Net 1.1版本未曾实现的Win32 API呢?这还需要我们慢慢的去发掘啊。