I can view a remotly connected pc from this article:Remote Desktop using c-net . but i dont need it. I just have to connect with that pc and get the free space data of C drive. How could i do this? I can connect to a remote desktop. I can get driveInfo using IO namespace. but how to combine them?
-
Why use a remote desktop client if you don't need to remote desktop? I'd suggest looking at WMI for this - see [this question](http://stackoverflow.com/q/12603641/1220971) for how. – Bridge Jan 21 '13 at 16:33
3 Answers
Use the System.Management
namespace and Win32_Volume
WMI class for this. You can query for an instance with a DriveLetter
of C:
and retrieve its FreeSpace
property as follows:
ManagementPath path = new ManagementPath() {
NamespacePath = @"root\cimv2",
Server = "<REMOTE HOST OR IP>"
};
ManagementScope scope = new ManagementScope(path);
string condition = "DriveLetter = 'C:'";
string[] selectedProperties = new string[] { "FreeSpace" };
SelectQuery query = new SelectQuery("Win32_Volume", condition, selectedProperties);
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
using (ManagementObjectCollection results = searcher.Get())
{
ManagementObject volume = results.Cast<ManagementObject>().SingleOrDefault();
if (volume != null)
{
ulong freeSpace = (ulong) volume.GetPropertyValue("FreeSpace");
// Use freeSpace here...
}
}
There is also a Capacity
property that stores the total size of the volume.

- 15,725
- 6
- 48
- 68
-
1
-
2That depends on your environment. Credentials and other security options can be set using the [`scope.Options` property](http://msdn.microsoft.com/library/system.management.managementscope.options.aspx), which is an instance of the [`ConnectionOptions` class](http://msdn.microsoft.com/library/system.management.connectionoptions.aspx). – Lance U. Matthews Jan 21 '13 at 17:40
-
-
I found out it's in Byte. but the information isn't accurate. For example the C: drive has 56.5 GB free in the server but the code is showing me 62GB. – Si8 Sep 08 '17 at 13:43
-
2@Si8 Where are you getting the "accurate" free space from? Windows Explorer? Are you calculating free gigabytes using `freeSpace / 1024 / 1024 / 1024` or, I presume, `freeSpace / 1000 / 1000 / 1000`? – Lance U. Matthews Sep 08 '17 at 15:04
-
Yes... I +1 because I was using Google's converter it was not working correctly but when I applied the proper calculations it worked. Thanks. – Si8 Sep 08 '17 at 15:56
After losing a full day trying to make WMI work remotely without success I discovered an alternative using performance counters. Simply check the Free Megabytes
counter in the LogicalDisk
category using the desired drive letter (appended by ":") as the instance name to get an updated reading of the drive's available free space:
"LogicalDisk(C:)\Free Megabytes"
You can access it programmatically in C# through the PerformanceCounter Class.
For accessing it remotely you'll need to specify the server name to the performance counter class constructor and the impersonated account must be added to the "Performance Monitor Users" group:
net localgroup "Performance Monitor Users" %username% /add

- 13,819
- 3
- 50
- 44
-
It'd be helpful, if not necessary, to provide the `PerformanceCounter` code that queries this value. – Lance U. Matthews Sep 30 '21 at 21:20
Here is the vb.net equivalent in case you need to translate it.
Dim path = New ManagementPath With {.NamespacePath = "root\cimv2",
.Server = "<REMOTE HOST OR IP>"}
Dim scope = New ManagementScope(path)
Dim condition = "DriveLetter = 'C:'"
Dim selectedProperties = {"FreeSpace"}
Dim query = New SelectQuery("Win32_Volume", condition, selectedProperties)
Dim searcher = New ManagementObjectSearcher(scope, query)
Dim results = searcher.Get()
Dim volume = results.Cast(Of ManagementObject).SingleOrDefault()
If volume IsNot Nothing Then
Dim freeSpace As ULong = volume.GetPropertyValue("FreeSpace")
End If

- 369
- 3
- 6