Determining a pc's memory & disk space
-
Hi, Can anyone tell me how I can determine how much memory, disk space, and free disk space are on a computer programmatically? This will be displayed on a windows form. I'm thinking it's got to be in one of the dlls that come with windows, but I don't know which one and what functions to use. Thanks! :)
-
Hi, Can anyone tell me how I can determine how much memory, disk space, and free disk space are on a computer programmatically? This will be displayed on a windows form. I'm thinking it's got to be in one of the dlls that come with windows, but I don't know which one and what functions to use. Thanks! :)
Instead of P/Invoking native methods, this is a perfect opportunity to use WMI (see the
System.Management
namespace). In fact, there's already several great tools to help you. This also allows you to query remote computers where the native functions won't always allow you to do that. VS.NET makes it even easier. Show your Server Explorer tab, expand your computer, then Management Classes, then My Computer and select your computer. You'll notice the property grid has a lot of information you want. Simply drag that to your form and it creates an easy-to-use wrapper class for querying that management information. You can change the computer name and get that information for other computers as well. For disk infomration, you should've noticed the Disk Volumes class right above My Computer. You can create a management class wrapper for that, too, and then specify the path and get the drive's information. You could use the following native functions, but querying information for other computers isn't as easy (GetDiskFreeSpaceEx
can use UNC paths, but they must have a trailing backslash (\) and require a share on the machine to query):[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetDiskFreeSpaceEx(string root,
out long freeBytesToCaller,
out long totalBytes,
out long freeBytes);
[DllImport("kernel32.dll")]
public static extern void GlobalMemoryStatus(out MemoryStatus status);
[StructLayout(LayoutKind.Sequential)]
public struct MemoryStatus
{
public int length;
public int memoryLoad;
public long totalPhysical;
public long availablePhysical;
public long totalPageFile;
public long availablePageFile;
public long totalVirtual;
public long availableVirtual;
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Hi, Can anyone tell me how I can determine how much memory, disk space, and free disk space are on a computer programmatically? This will be displayed on a windows form. I'm thinking it's got to be in one of the dlls that come with windows, but I don't know which one and what functions to use. Thanks! :)
Look at the System.Management namespace ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); object freeSpace = disk.Properties["FreeSpace"].Value; Jonathan
-
Instead of P/Invoking native methods, this is a perfect opportunity to use WMI (see the
System.Management
namespace). In fact, there's already several great tools to help you. This also allows you to query remote computers where the native functions won't always allow you to do that. VS.NET makes it even easier. Show your Server Explorer tab, expand your computer, then Management Classes, then My Computer and select your computer. You'll notice the property grid has a lot of information you want. Simply drag that to your form and it creates an easy-to-use wrapper class for querying that management information. You can change the computer name and get that information for other computers as well. For disk infomration, you should've noticed the Disk Volumes class right above My Computer. You can create a management class wrapper for that, too, and then specify the path and get the drive's information. You could use the following native functions, but querying information for other computers isn't as easy (GetDiskFreeSpaceEx
can use UNC paths, but they must have a trailing backslash (\) and require a share on the machine to query):[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetDiskFreeSpaceEx(string root,
out long freeBytesToCaller,
out long totalBytes,
out long freeBytes);
[DllImport("kernel32.dll")]
public static extern void GlobalMemoryStatus(out MemoryStatus status);
[StructLayout(LayoutKind.Sequential)]
public struct MemoryStatus
{
public int length;
public int memoryLoad;
public long totalPhysical;
public long availablePhysical;
public long totalPageFile;
public long availablePageFile;
public long totalVirtual;
public long availableVirtual;
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Heath, Thanks for your help, but I can not find the System.Management namespace. Where is this namespace located and or how do I add it? I tried using System.Management; but that doesn't work. Thanks