C#檢查遠(yuǎn)程或本地磁盤使用率
更新時間:2016年04月28日 14:04:23 作者:BZindex
要檢查磁盤的使用情況確定程序放哪個服務(wù)器和清理垃圾,所以寫個小程序幫忙檢查。本文給大家介紹C#檢查遠(yuǎn)程或本地磁盤使用率的相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧
因?yàn)楣居卸鄠€服務(wù)器,要檢查磁盤的使用情況確定程序放哪個服務(wù)器和清理垃圾,所以寫個小程序幫忙檢查。
效果圖:


后臺代碼:
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("請把信息補(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);//連接時間
//ManagementScope 的服務(wù)器和命名空間。
string path = string.Format(@"\\{0}\root\cimv2", 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 += " ----請盡快清理!";
}
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# winForm自定義鼠標(biāo)樣式的兩種實(shí)現(xiàn)方法詳解
本篇文章是對在C#中winForm自定義鼠標(biāo)樣式的兩種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#中計(jì)數(shù)排序算法的原理及實(shí)現(xiàn)
計(jì)數(shù)排序是一種線性時間復(fù)雜度的排序方法,主要通過統(tǒng)計(jì)元素出現(xiàn)的次數(shù)實(shí)現(xiàn)排序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
C#使用ZXing.Net實(shí)現(xiàn)識別二維碼和條碼
ZXing用Java實(shí)現(xiàn)的多種格式的一維二維條碼圖像處理庫,而ZXing.Net是其.Net版本的實(shí)現(xiàn),本文主要為大家詳細(xì)介紹了如何使用ZXing.Net實(shí)現(xiàn)識別二維碼和條碼,需要的可以參考下2024-01-01
將c#編寫的程序打包成應(yīng)用程序的實(shí)現(xiàn)步驟分享(安裝,卸載) 圖文
時常會寫用c#一些程序,但如何將他們和photoshop一樣的大型軟件打成一個壓縮包,以便于發(fā)布.2011-12-12
C#實(shí)現(xiàn)排列組合算法完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)排列組合算法的完整實(shí)例,文中實(shí)例主要展示了排列循環(huán)方法和排列堆棧方法,需要的朋友可以參考下2014-09-09
c#生成站點(diǎn)地圖(SiteMapPath)文件示例程序
這篇文章主要介紹了c#生成站點(diǎn)地圖(SiteMapPath)文件的示例,大家參考使用2013-11-11
c#遠(yuǎn)程html數(shù)據(jù)抓取實(shí)例分享
這篇文章主要介紹了c#遠(yuǎn)程html數(shù)據(jù)抓取的方法,大家參考使用吧2013-12-12

