Getting drive info from a remote computer

China☆狼群 提交于 2019-12-10 15:11:20

问题


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?


回答1:


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.




回答2:


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



回答3:


You can collect remote pc information by using WMI, but it requires RPC to be running on the remote pc.

Please take a look at these links: http://www.codeproject.com/Articles/8804/Collecting-Remote-System-Information-With-WMI and http://blogs.msdn.com/b/securitytools/archive/2009/07/29/wmi-programming-using-c-net.aspx



来源:https://stackoverflow.com/questions/14442960/getting-drive-info-from-a-remote-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!