Drive Information

To obtain information about the various drives in your computer you can use the DriveInfo class. This class contains all sorts of information regarding the drive it references. For this example I will just show you how to obtain the total and free space in Gigabytes for the drive. In this example I will be retrieving info about the C: drive. Also, don’t forget that the sizes returned are in bytes and you should convert them to more meaningful types. Quick info about byte conversion:

Physical Memory

To obtain information about your computer’s physical memory you have two options. You can use P/Invoke, which offers much better performance, or WMI for .NET. I will show you using P/Invoke because it’s faster and because I grew up with Win32 and C++ and have sentimental attachment to it. There is a little bit of funkiness to obtaining information about your physical memory in that there are multiple ways to obtain the available memory but only two ways to obtain the total memory (which are p/invoke and WMI). If all you need to know is the amount of available physical memory you can use the PerformanceCounter class which doesn’t require p/invoke but does come with a serious performance hit. Example:

var memCounter = new PerformanceCounter("Memory", "Available MBytes");
var available = Math.Round(memCounter.NextValue() / Math.Pow(2, 10), 2); // Because our performance counter returns the available memory in MB we only need to divide by one magnitude to get to GB

If you want to know both available and physical then you would use p/invoke and Win32 as so:

var Available = Math.Round(Win32Helper.GetAvailablePhysicalMemory() / Math.Pow(2, 30), 2); // I like my available to be semi-accurate so I only round to two decimals
var Total = Math.Round(Win32Helper.GetTotalPhysicalMemory() / Math.Pow(2, 30), 0, MidpointRounding.AwayFromZero); // I like my total to be a nice round number so I round up and chop the decimals