C#获取磁盘空间信息的几个方法

标签: 磁盘c#stringsystemdlldisk
5002人阅读 评论(0) 收藏 举报
分类:

    今天本来想找一个在磁盘拷贝的过程中快速获取磁盘大小信息的方法,可惜这几个方法都不给力,拷贝中获取信息依然很慢。纠结。
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);

            List<ulong> driveInfoList = new List<ulong>(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://download.csdn.net/detail/yysyangyangyangshan/4166834

 

9
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1810931次
    • 积分:23218
    • 等级:
    • 排名:第308名
    • 原创:396篇
    • 转载:19篇
    • 译文:0篇
    • 评论:493条
    博客专栏
    最新评论