Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out total and free disk space in .NET

I am trying to find a way to determine the total and available disk space in an arbitrary folder from a .NET app. By "total disk space" and "available disk space" in a folder I refer to the total and available disk space that this folder would report if you performed a "dir" command on it, that is, the total and available disk space of the logical drive containing that folder, considering the user account under which the request is being made.

I am using C#. The method should work both for local and remote folders given as UNC paths (rather than accessed through mapped drive letters). For example, it should work for:

  • C:\Temp
  • \\Silfen\Resources\Temp2

I am starting with a DirectoryInfo object, but this seems to have no associated disk space information. The DriveInfo class does, but it won't work with remote folders.

Edit. After some exchanges with you guys, I am considering mapping remote folders as local drives, using DriveInfo to obtain the data, and unmapping again. The problem with this approach is that my app needs to collect the data for over 120 folders a few times a day, every day. I am not sure this would be feasible.

Any ideas? Thanks.

like image 795
CesarGon Avatar asked Nov 25 '09 21:11

CesarGon


People also ask

How do I check the total disk space?

How will I know how much space I have left? To check the total disk space left on your Windows 10 device, select File Explorer from the taskbar, and then select This PC on the left. The available space on your drive will appear under Devices and drives.

Which command displays the amount of free disk space?

Use the df command to show the amount of free disk space on each mounted disk.

How do I check free space on Windows Server?

Click Select counters from computer, and then select your computer in the list. In the Performance object box, click LogicalDisk. Click Select counters from list, and then click % Free Space. Click Select interfaces from list, and then click the logical drive or volume that you want to monitor.


1 Answers

How about this link from MSDN that uses the System.IO.DriveInfo class?

like image 86
t0mm13b Avatar answered Oct 01 '22 06:10

t0mm13b