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