从远程计算机获取驱动器信息

从远程计算机获取驱动器信息

本文介绍了从远程计算机获取驱动器信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过这篇文章查看远程连接的电脑:使用 c-net 的远程桌面.但我不需要它.我只需要与那台电脑连接并获取C盘的可用空间数据.我怎么能这样做?我可以连接到远程桌面.我可以使用 IO 命名空间获取 driveInfo.但如何将它们结合起来?

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?

推荐答案

使用系统.Management 命名空间Win32_Volume WMI 类 为此.您可以使用 C:DriveLetter 查询实例并检索其 FreeSpace 属性,如下所示:

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...
    }
}

还有一个 Capacity 属性,用于存储卷的总大小.

There is also a Capacity property that stores the total size of the volume.

这篇关于从远程计算机获取驱动器信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:19