C#擷取本地或遠程磁碟使用資訊

來源:互聯網
上載者:User

標籤:each   images   mes   blog   desc   指定   box   end   root   

因為公司有多個伺服器,要檢查磁碟的使用方式確定程式放哪個伺服器和清理垃圾,所以寫個小程式幫忙檢查。

:

 

後台代碼:

 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#擷取本地或遠程磁碟使用資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.