C # Check remote or Local disk usage _c# tutorial

Source: Internet
Author: User
Tags disk usage

Because the company has multiple servers, to check the use of the disk to determine which server the program to put and clean the garbage, so write a small program to help check.

Effect Chart:

Background code:

private void btnCheck_Click (object sender, EventArgs e) {listBox1.Items.Clear (); if (rbtnremote.checked) {//Remote Remotedi
SK ();
else {//local localdisk ();}}
View local private void Localdisk () {wqlobjectquery wmiquery = new Wqlobjectquery ("SELECT * from Win32_LogicalDisk");
ManagementObjectSearcher wmifind = new ManagementObjectSearcher (wmiquery);
Display Showinfo (wmifind); //Remote private void Remotedisk () {if (Cbip.text = = "" | Cbusername.text = "" |
Txtpwd.text = = "") {MessageBox.Show ("Please add the information complete!");
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);//connection time//managementscope server and namespace. String path = string.
Format (@ "\\{0}\root\cimv2", IP); 
Represents the scope (namespace) of an administrative operation, using the specified options to initialize a new instance of the Managementscope class that represents the specified range path. 
Managementscope scope = new Managementscope (PATH, conn); Scope. 
Connect (); Query string strquery = "SelecT * from Win32_LogicalDisk ";
ObjectQuery query = new ObjectQuery (strquery);
Query Managementobjectcollection Returns the result set ManagementObjectSearcher Wmifind = new ManagementObjectSearcher (scope, query);
Showinfo (Wmifind); 
#region Display disk information 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 (); Determine if this is a native fixed disk if (type = = "Local Fixed disk") {str = "disk name:" + mobj["name"].
ToString ();
Freepath = Math.Round (convert.todouble (mobj["FreeSpace"))/GB, 1);
STR + + "free space:" + freepath+ "G"; str + = "Actual Size:" + Math.Round (convert.todouble (mobj["size").
ToString ())/GB, 1) + "G";
if (Freepath <) {str = "----Please clean up as soon as possible!";} listBox1.Items.Add (str); 
#endregion private void Rbtnlocal_checkedchanged (object sender, EventArgs e) {//locally Select 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 = t
Rue
Cbusername.enabled = true;
Txtpwd.enabled = true; }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.