查看硬盘空间

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;

using yy_checkerLib;
//using ServerDll
using SICTS_Server;
namespace jsTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static string GetSizeUseUnit(string size)
        {
            double dSpace = Convert.ToDouble(size);
            string sSpace = dSpace.ToString("N");
            string[] tmp;
            string rtnSize = "0";
            tmp = sSpace.Split(',');
            switch (tmp.GetUpperBound(0))
            {
                case 0:
                    rtnSize = tmp[0] + " 字节";
                    break;
                case 1:
                    rtnSize = tmp[0] + "." + tmp[1].Substring(0, 2) + " K";
                    break;
                case 2:
                    rtnSize = tmp[0] + "." + tmp[1].Substring(0, 2) + " M";
                    break;
                case 3:
                    rtnSize = tmp[0] + "." + tmp[1].Substring(0, 2) + " G";
                    break;
                case 4:
                    rtnSize = tmp[0] + "." + tmp[1].Substring(0, 2) + " T";
                    break;
            }
            return rtnSize;
        }

        private void GetInfo()
        {
            try
            {
                ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
                ManagementObjectCollection drives = diskClass.GetInstances();
                string imageSavePath = @"E:\SICTS\image_SIC\";
                foreach (ManagementObject drv in drives)
                {
                    if (imageSavePath.StartsWith(drv["Name"].ToString()))
                    {
                        tsbText.Text = "存图空间报告--------截止" +
                            DateTime.Now.ToString("MM月dd日HH时mm分,") +
                            drv["Name"].ToString() + "尚有 " +
                            GetSizeUseUnit(drv["FreeSpace"].ToString()) + " 空间未使用!";
                        this.tsbText.TextAlign = ContentAlignment.MiddleRight;
                        this.tsbText.ImageAlign = ContentAlignment.BottomRight;
                        this.tsbText.TextImageRelation = TextImageRelation.TextBeforeImage;
                        this.tsbText.Spring = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
               // Log.WriteToFile(4, "获取失败", "获取存图空间错误,详细情况:" + ex.Message);
            }
        }
        //公司的方法
        public long GetDiskSpace(string strpath)
        {
            long lfreeBytesAvailable = 0;
            long lTotalNumberOfBytes = 0;
            long lTotalNumberOfFreeBytes = 0;
            long lfreeMByteAvailable = 0;
            int index = strpath.IndexOf(@"\");

            try
            {
                if (index > 0)
                {
                    string strImageRootpath = strpath.Substring(0, index);
                    ServerDll.GetDiskFreeSpaceEx(strImageRootpath, out lfreeBytesAvailable, out lTotalNumberOfBytes, out lTotalNumberOfFreeBytes);
                    lfreeMByteAvailable = 0;
                    lfreeMByteAvailable = lfreeBytesAvailable / 1024 / 1024 / 1024;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //Log.WriteToFile("获取磁盘可用空间出错——>", ex.Message);
            }
            return lfreeMByteAvailable;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           // GetInfo();
            long  nn=GetDiskSpace(@"E:\SICTS\image_SIC\");
            MessageBox.Show(nn.ToString());
        }
    }
}

原文地址:https://www.cnblogs.com/xianqingzh/p/1774504.html