我可以通过文章:Remote Desktop using c-net查看远程连接的电脑。但我不需要它。我只需连接该PC并获取C盘的可用空间数据。我怎么能这样做?我可以连接到远程桌面。我可以使用IO命名空间获取driveInfo。但如何将它们结合起来?
答案 0 :(得分:19)
使用System.Management
namespace和Win32_Volume
WMI class进行此操作。您可以查询DriveLetter
C:
FreeSpace
的实例并检索其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...
}
}
属性,如下所示:
Capacity
还有一个{{1}}属性存储卷的总大小。
答案 1 :(得分:1)
以下是vb.net等效内容,以便您需要翻译它。
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
答案 2 :(得分:0)