因为公司有多个服务器,要检查磁盘的使用情况确定程序放哪个服务器和清理垃圾,所以写个小程序帮忙检查。
效果图:
后台代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
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 ;
} } |