手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#检查远程或本地磁盘使用率
C#检查远程或本地磁盘使用率
摘要:因为公司有多个服务器,要检查磁盘的使用情况确定程序放哪个服务器和清理垃圾,所以写个小程序帮忙检查。效果图:后台代码:privatevoidb...

因为公司有多个服务器,要检查磁盘的使用情况确定程序放哪个服务器和清理垃圾,所以写个小程序帮忙检查。

效果图:

C#检查远程或本地磁盘使用率1

C#检查远程或本地磁盘使用率2

后台代码:

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}rootcimv2", 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#检查远程或本地磁盘使用率】相关文章:

c# 随机函数的使用详解

深入多线程之:内存栅栏与volatile关键字的使用分析

C#中控制远程计算机的服务的方法

C#数据结构揭秘一

C#可选参数的相关使用

探讨如何用委托处理排序

C#多维数组学习使用

C#利用子线程刷新主线程分享教程

C#日期转换函数分享

C#中把日志导出到txt文本的简单实例

精品推荐
分类导航