C#判斷磁盤剩余空間是否足夠


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;
        }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM