Finding out total and free disk space in .NET

CesarGon picture CesarGon · Nov 25, 2009 · Viewed 32.4k times · Source

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.

Answer

t0mm13b picture t0mm13b · Nov 25, 2009

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