C#檢討長途或當地磁盤應用率。本站提示廣大學習愛好者:(C#檢討長途或當地磁盤應用率)文章只能為提供參考,不一定能成為您想要的結果。以下是C#檢討長途或當地磁盤應用率正文
由於公司有多個辦事器,要檢討磁盤的應用情形肯定法式放哪一個辦事器和清算渣滓,所以寫個小法式協助檢討。
後果圖:
後台代碼:
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; } }