.Net2.0中的DriveInfo类

.Net2.0中的DriveInfo类


  作者: wayfarer 来源: 博客园  在.Net 1.1中,要获得磁盘信息,只有通过Win32的API来获得,例如: 获取磁盘剩余空间;

<[DllImport( "kernel32.dll", EntryPoint="GetDiskFreeSpaceA" )]

public static extern int GetDiskFreeSpace(string lpRootPathName,ref int lpSectorsPerCluster,

                                                                                                            ref int lpBytesPerSector,

                                                                                                            ref int lpNumberOfFreeClusters,

                                                                                                            ref int lpTotalNumberOfClusters);



//获取磁盘类型;

[DllImport( "kernel32.dll", EntryPoint="GetDriveTypeA" )]

public static extern int GetDriveType(string nDrive);

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

using System.IO;



string driveName = "C:\\";

DriveInfo driveInfo = new DriveInfo(driveName);



Console.WriteLine("The volume name is {0}",driveInfo.VolumeLabel);

Console.WriteLine("The total space is {0}",driveInfo.TotalSize);

Console.WriteLine("The free space is {0}",driveInfo.TotalFreeSpace);另外,DriveInfo类还有一个静态方法GetDrives(),它能获得当前计算机所有驱动器的信息: 

DriveInfo[] drives = DriveInfo.GetDrives();不知道.Net2.0又封装了多少.Net 1.1版本未曾实现的Win32 API呢?这还需要我们慢慢的去发掘啊。  (2005-7-07:07:40)

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [Visual Studio.NET] 的这些文章也感兴趣:

Visual Studio 2010和.NET Framework 4.0
MSDN Code Gallery Goes Live
ORACLE发布新的.NET驱动和开发插件
使用VisualStudio 2010从分析到实施(4)——使用Sequence Diagram设计消息序列
Web中使用多线程来增强用户体验
VS 2008 Web部署项目支持发布了
Windows Will Soon Enter Into the EC2 Cloud
.Net类库中实现的HashTable
利用Mono-cecil实现.NET程序的重新签名,重新链接相关库的引用
BlogEngine.Net架构与源代码分析系列part6:开放API——MetaWeblog与BlogImporter