因?yàn)楣居卸鄠€(gè)服務(wù)器,要檢查磁盤的使用情況確定程序放哪個(gè)服務(wù)器和清理垃圾,所以寫個(gè)小程序幫忙檢查。
效果圖:
?
后臺(tái)代碼:
private void btnCheck_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
if (rbtnRemote.Checked)
{
//遠(yuǎn)程
RemoteDisk();
}
else
{
//本地
LocalDisk();
}
}
//查看本地
private void LocalDisk()
{
WqlObjectQuery wmiquery = new WqlObjectQuery("select * from Win32_LogiCalDisk");
ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery);
//顯示
ShowInfo(wmifind);
}
//遠(yuǎn)程
private void RemoteDisk()
{
if (cbIP.Text == "" | cbUserName.Text == "" | txtPwd.Text == "")
{
MessageBox.Show("請(qǐng)把信息補(bǔ)充完整!");
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);//連接時(shí)間
//ManagementScope 的服務(wù)器和命名空間。
string path = string.Format(@"\{0} ootcimv2", ip);
//表示管理操作的范圍(命名空間),使用指定選項(xiàng)初始化ManagementScope 類的、表示指定范圍路徑的新實(shí)例。
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();
//判斷是否是本機(jī)固盤
if (type == "Local Fixed Disk")
{
str = " 磁盤名稱:" + mobj["Name"].ToString();
freePath = Math.Round(Convert.ToDouble(mobj["FreeSpace"]) / gb, 1);
str += " 可用空間:" + freePath+ "G";
str += " 實(shí)際大小:" + Math.Round(Convert.ToDouble(mobj["Size"].ToString()) / gb, 1) + "G";
if (freePath < 20)
{
str += " ----請(qǐng)盡快清理!";
}
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;
}
}
?
本文摘自 :https://blog.51cto.com/u