杨友山

专注于C#.NET程序开发;为开发出完美的软件而奋斗!yangyoushan123@163.com ;379475542...

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

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

 

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yysyangyangyangshan/article/details/7387864
个人分类: c#程序设计
上一篇C#中程序的退出
下一篇用心做软件—细节决定成败
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭