C#检查远程或本地磁盘使用率
程序员文章站
2022-06-05 14:23:26
因为公司有多个服务器,要检查磁盘的使用情况确定程序放哪个服务器和清理垃圾,所以写个小程序帮忙检查。
效果图:
后台代码:
privat...
因为公司有多个服务器,要检查磁盘的使用情况确定程序放哪个服务器和清理垃圾,所以写个小程序帮忙检查。
效果图:
后台代码:
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; } }
上一篇: C# MVC模式下商品抽奖功能实现
下一篇: C#微信开发之微信公众号标签管理功能