C#檢查遠程或本地磁盤使用率
更新時間:2016年04月28日 14:04:23 作者:BZindex
要檢查磁盤的使用情況確定程序放哪個服務(wù)器和清理垃圾,所以寫個小程序幫忙檢查。本文給大家介紹C#檢查遠程或本地磁盤使用率的相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧
因為公司有多個服務(wù)器,要檢查磁盤的使用情況確定程序放哪個服務(wù)器和清理垃圾,所以寫個小程序幫忙檢查。
效果圖:
后臺代碼:
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 的服務(wù)器和命名空間。 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返回結(jié)果集 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; } }
相關(guān)文章
C#多線程學(xué)習(xí)之(三)生產(chǎn)者和消費者用法分析
這篇文章主要介紹了C#多線程學(xué)習(xí)之生產(chǎn)者和消費者用法,實例分析了C#中線程沖突的原理與資源分配的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04C#中DataSet,DataTable,DataView的區(qū)別與用法
這篇文章介紹了C#中DataSet,DataTable,DataView的區(qū)別與用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05