zoukankan      html  css  js  c++  java
  • .Net 2.0中的DriveInfo类

    在.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呢?这还需要我们慢慢的去发掘啊。
  • 相关阅读:
    剔除数组中重复对象
    2018湖南省高中数学联赛(A)试题
    向量与三角
    阿波罗尼斯圆
    2019高考理科数学(天津卷)
    转载Android常见错误(http://www.cnblogs.com/oklanyan/archive/2011/10/12/2208378.html)
    TCP/IP网络层谜云
    bitmap 设置图片尺寸,避免 内存溢出 OutOfMemoryError的优化方法(转载:http://tonyyu.iteye.com/blog/713256)
    ArcGIS for Android 离线数据编辑原理
    Android环境搭建(XP/Win7)转载http://www.cnblogs.com/oklanyan/archive/2011/10/14/2207339.html
  • 原文地址:https://www.cnblogs.com/wayfarer/p/221795.html
Copyright © 2011-2022 走看看