conn);scope.Connect();//查询string strQuery = "select * from

时间:2021-07-31 07:27:37

因为公司有多个处事器,要查抄磁盘的使用情况确定措施放哪个处事器和清理垃圾,所以写个小措施资助查抄。

效果图:

conn);scope.Connect();//查询string strQuery = "select * from

conn);scope.Connect();//查询string strQuery = "select * from

后台代码:

private void btnCheck_Click(object sender, EventArgs e) { listBox1.Items.Clear(); if (rbtnRemote.Checked) { //长途 RemoteDisk(); } else { //本地 LocalDisk(); } } //检察本地 private void LocalDisk() { WqlObjectQuery wmiquery = new WqlObjectQuery("select * from Win32_LogiCalDisk"); ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery); //显示 ShowInfo(wmifind); } //长途 private void RemoteDisk() { if (cbIP.Text == "" | cbUserName.Text == "" | txtPwd.Text == "") { MessageBox.Show("请把信息增补完整!"); return; } string ip = cbIP.Text; string username = cbUserName.Text; string password = txtPwd.Text; ConnectionOptions conn = new ConnectionOptions(); conn.Username = username; conn.Password = password; conn.Timeout = new TimeSpan(1,1,1,1);//连接时间 //ManagementScope 的处事器和定名空间。 string path = string.Format(@"\\{0}\root\cimv2", ip); //暗示打点操纵的范畴(定名空间),使用指定选项初始化ManagementScope 类的、暗示指定范畴路径的新实例。 ManagementScope scope = new ManagementScope(path, conn); scope.Connect(); //盘问 string strQuery = "select * from Win32_LogicalDisk "; ObjectQuery query = new ObjectQuery(strQuery); //盘问ManagementObjectCollection返回功效集 ManagementObjectSearcher wmifind = new ManagementObjectSearcher(scope, query); ShowInfo(wmifind); } #region 显示磁盘信息 private void ShowInfo(ManagementObjectSearcher wmifind) { long gb = 1024 * 1024 * 1024; string type = ""; string str = ""; double freePath = 0d; foreach (var mobj in wmifind.Get()) { type = mobj["Description"].ToString(); //判断是否是本机固盘 if (type == "Local Fixed Disk") { str = " 磁盘名称:" + mobj["Name"].ToString(); freePath = Math.Round(Convert.ToDouble(mobj["FreeSpace"]) / gb, 1); str += " 可用空间:" + freePath+ "G"; str += " 实际巨细:" + Math.Round(Convert.ToDouble(mobj["Size"].ToString()) / gb, 1) + "G"; if (freePath < 20) { str += " ----请尽快清理!"; } listBox1.Items.Add(str); } } } #endregion private void rbtnLocal_CheckedChanged(object sender, EventArgs e) { //本地选中 if (rbtnLocal.Checked == true) { cbIP.Enabled = false; cbUserName.Enabled = false; txtPwd.Enabled = false; } } private void rbtnRemote_CheckedChanged(object sender, EventArgs e) { if (rbtnRemote.Checked == true) { cbIP.Enabled = true; cbUserName.Enabled = true; txtPwd.Enabled = true; } }