C# 获取磁盘空间信息

获取本地或远程服务器磁盘用度,示例如下

1.获取本机磁盘用度

  可通过 DriveInfo 或ManagementObjectSearcher两种方式

  A.DriveInfo 

 public void GetDriver()
        {            
            long lsum = 0, ldr = 0;
            long gb = 1024 * 1024 * 1024;

            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                //判断是否是固定磁盘
                if (drive.DriveType == DriveType.Fixed)
                {
                    lsum = drive.TotalSize / gb;
                    ldr = drive.TotalFreeSpace / gb;
                    richTextBox1.Text += drive.Name + ": 总空间=" + lsum.ToString() + " 剩余空间=" + ldr.ToString()+"\n\r";
                }                
            }
            //progressBar1.Value = int.Parse((lsum - ldr).ToString());
            //progressBar1.Maximum = int.Parse(lsum.ToString());
            //lbMsg.Text = "磁盘" + disksrc + "的可用空间为" + ldr + "GB!";
        }

 B.ManagementObjectSearcher

需引入命名空间:using System.Management;(同时在项目中,通过添加引用方式添加)
public 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 driveInfoList = new List(2);

            foreach (ManagementObject mobj in managerSearch.Get())
            {
                richTextBox1.Text += "Description: " + mobj["Description"] + "\r\n";
                richTextBox1.Text += "File system: " + mobj["FileSystem"] + "\r\n";
                richTextBox1.Text += "Free disk space: " + mobj["FreeSpace"] + "\r\n";
                richTextBox1.Text += "Size: " + mobj["Size"] + "\r\n";
            }

        }
调用示例:
GetDriveInfoDetail("D:");//注意盘符后要有:号

 
  

2.获取远程服务器磁盘用度

 private void button2_Click(object sender, EventArgs e)
        {
            string ip = textBoxSrcPath.Text;//"172.16.5.214";

            string disksrc = textBoxDirPath.Text + ":";//"D:";

            string username = txtUsername.Text;//"Administrator"; //

            string password = txtPassword.Text;//"password";


            long freesize = 0, size = 0;

            long gb = 1024 * 1024 * 1024;

            ConnectionOptions connectionOptions = new ConnectionOptions();

            connectionOptions.Username = username;

            connectionOptions.Password = password;

            connectionOptions.Timeout = new TimeSpan(1, 1, 1, 1);//连接时间



            //ManagementScope 的服务器和命名空间。

            string path = string.Format("\\\\{0}\\root\\cimv2", ip);

            //表示管理操作的范围(命名空间),使用指定选项初始化ManagementScope 类的、表示指定范围路径的新实例。

            ManagementScope scope = new ManagementScope(path, connectionOptions);

            scope.Connect();

            //查询字符串,某磁盘上信息

            string strQuery = string.Format("select * from Win32_LogicalDisk where deviceid='{0}'", disksrc);
            

            ObjectQuery query = new ObjectQuery(strQuery);

            //查询ManagementObjectCollection返回结果集
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            foreach (ManagementObject m in searcher.Get())
            {
                if (m["Name"].ToString() == disksrc)
                {   //通过m["属性名"]
                    freesize = Convert.ToInt64(m["FreeSpace"]) / gb;
                    size = Convert.ToInt64(m["Size"]) / gb;
                }
            }
            txtdr.Text = freesize.ToString();
            if (freesize <= 20)
            {
                lbMsg.ForeColor = Color.Red;
                lbMsg.Text = "磁盘" + disksrc + "的可用空间为" + freesize + "GB,请及时清理!";
            }

            progressBar1.Maximum = int.Parse(size.ToString());
            progressBar1.Value = int.Parse((size - freesize).ToString());

            lbMsg.Text = "磁盘" + disksrc + "的可用空间为" + freesize + "GB!";            
        }


你可能感兴趣的:(.Net)