本文共 1746 字,大约阅读时间需要 5 分钟。
今天本来想找一个在磁盘拷贝的过程中快速获取磁盘大小信息的方法,可惜这几个方法都不给力,拷贝中获取信息依然很慢。纠结。
1、C#DriveInfo类获取DriveInfo driveinfo = new DriveInfo(@"C:\"); driveinfo.AvailableFreeSpace; driveinfo.TotalSize;
2、调用系统DLL方法
[DllImport("kernel32.dll")] public static extern bool GetDiskFreeSpaceEx( string lpDirectoryName, out UInt64 lpFreeBytesAvailable, out UInt64 lpTotalNumberOfBytes, out UInt64 lpTotalNumberOfFreeBytes);
调用
public static void GetDriveInfoDetail(string driveName) { ulong availablefreeBytes = 0; ulong totalNumberOfBytes = 0; ulong totalNumberOfFreeBytes = 0; GetDiskFreeSpaceEx( driveName, out availablefreeBytes, out totalNumberOfBytes, out totalNumberOfFreeBytes); }
3、ManagementObjectSearcher查询方法
public static void GetDriveInfoDetail(string driveName) { WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = '{0}'", driveName.Substring(0, 2))); ManagementObjectSearcher managerSearch = new ManagementObjectSearcher(wqlObjectQuery); ListdriveInfoList = new List (2); foreach (ManagementObject mobj in managerSearch.Get()) { Console.WriteLine("Description: " + mobj["Description"]); Console.WriteLine("File system: " + mobj["FileSystem"]); Console.WriteLine("Free disk space: " + mobj["FreeSpace"]); Console.WriteLine("Size: " + mobj["Size"]); } }
代码下载:
转载地址:http://icbmx.baihongyu.com/