C # How to get disk space size

Source: Internet
Author: User
Tags how to get disk space

Method One: Using the System.IO.DriveInfo.GetDrives method to obtain

Gets the total space size of the specified drive (in B)/////////////////or public static             Long Getharddiskspace (string str_harddiskname) {Long totalsize= new long ();             Str_harddiskname=str_harddiskname + ": \ \";             system.io.driveinfo[] drives = System.IO.DriveInfo.GetDrives (); foreach (System.IO.DriveInfo drive in drives) {if (drive. Name = = str_harddiskname) {totalsize = drive.                 TotalSize/(1024 * 1024 * 1024);         }} return totalsize; }//////////To get the total amount of space remaining for the specified drive (in B)///////public only by entering the letter representing the             Static long Getharddiskfreespace (string str_harddiskname) {Long freeSpace = new Long ();             Str_harddiskname = str_harddiskname + ": \ \";     system.io.driveinfo[] drives = System.IO.DriveInfo.GetDrives ();        foreach (System.IO.DriveInfo drive in drives) {if (drive. Name = = str_harddiskname) {freeSpace = drive.                 Totalfreespace/(1024 * 1024 * 1024);         }} return freeSpace; }

method Two: Use ManagementClass ("Win32_LogicalDisk") to get the

list<dictionary<string, string>> diskinfodic = new List<Dictionary< String, string>> (); ManagementClass Diskclass = new ManagementClass ("Win32_LogicalDisk"); managementobjectcollection disks = Diskclass.getinstances (); foreach (ManagementObject disk in disks) {dictionary< String, string> diskinfo = new dictionary<string, string> (), try{//disk name diskinfo["name"] =disk["name"]. ToString ();//Disk Description diskinfo["Description"]=disk["Description"]. ToString ();//disk total capacity, free space, used space if (System.Convert.ToInt64 (disk["Size"]) > 0) {Long totalspace = System.Convert.ToInt64 (disk["Size"])/Mb;long freeSpace = System.Convert.ToInt64 (disk["FreeSpace"])/Mb;long                        Usedspace = Totalspace-freespace; diskinfo["Totalspace"]=totalspace.tostring ();d iskinfo["Usedspace"]=usedspace.tostring ();d iskinfo["FreeSpace"]= Freespace.tostring ();} Diskinfodic.add (DiskInfo);} catch (Exception ex) {Throw ex;}}  

More IT related information and technical articles, welcome to my personal website: http://www.zuowenjun.cn/

C # How to get disk space size

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.