码迷,mamicode.com
首页 > Windows程序 > 详细

C#判断磁盘剩余空间是否足够

时间:2020-05-22 09:42:31      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:logic   save   result   get   lse   select   ica   else   tin   

public static bool GetDiskInfo()
        {
            if (false == strCheckFreeSpace.Equals("1"))
                return true;
            try
            {
                //获得盘符HARDDISK
                var index = strSaveFolder.Trim().IndexOf(‘:‘);
                var HARDDISK = strSaveFolder.Trim().Substring(0, index + 1);

                SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);
                ManagementObjectCollection diskcollection = searcher.Get();
                if (diskcollection != null && diskcollection.Count > 0)
                {
                    foreach (ManagementObject item in searcher.Get())
                    {
                        int nType = Convert.ToInt32(item["DriveType"]);
                        if (nType != Convert.ToInt32(DriveType.Fixed))
                        {
                            continue;
                        }
                        else
                        {
                            if (item["DeviceID"].ToString().ToUpper() == HARDDISK)
                            {
                                double bytes = Convert.ToDouble(item["FreeSpace"]);

                                var result = bytes / Math.Pow(1024, 3);

                                if (result < Int32.Parse(你想要规定的gb数))
                                {
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

  

C#判断磁盘剩余空间是否足够

标签:logic   save   result   get   lse   select   ica   else   tin   

原文地址:https://www.cnblogs.com/zmy2020/p/12935118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!