To get the free space of the system disk on a Windows machine using C#, you can use the DriveInfo
class in the System.IO
namespace.
main.cs80 chars3 lines
In the code above, we create a DriveInfo
object for the system disk with the path "C:\\"
and then retrieve the TotalFreeSpace
in bytes as a long
integer.
If you want to format the bytes as a human-readable string (e.g. "4.2 GB"), you can use the following method:
main.cs366 chars10 lines
Now you can use the FormatBytes
method to print the free disk space in a human-readable format:
main.cs60 chars2 lines
gistlibby LogSnag