如何查询远程计算机中的文件夹大小通过WMI和C# [英] How to query Folder Size in remote computer through WMI and C#
问题描述
如何通过WMI和C#查看远程计算机中的文件夹大小。
我需要找到每个用户的文件夹大小在C:\Users在远程系统通过WMI。
How to query Folder Size in remote computer through WMI and C#. I need to find the each User's folder size in C:\Users in remote System through WMI.
我试过Win32_Directory,CMI_DataFile但无法找到所需的答案。
请帮助!!
I tried Win32_Directory , CMI_DataFile but not able to find the desired answer. Please help!!
推荐答案
要使用WMI获取文件夹的大小,必须遍历文件使用 CIM_DataFile
类,然后从 FileSize
属性中获取每个文件的大小。
To get the size of a folder using the WMI, you must iterate over the files using the CIM_DataFile
class and then get the size of each file from the FileSize
property.
尝试这个示例(此代码不是递归的,我为您离开这样的任务)。
Try this sample (this code is not recursive, I leave such task for you).
using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
namespace GetWMI_Info
{
class Program
{
// Directory is a type of file that logically groups data files 'contained' in it,
// and provides path information for the grouped files.
static void Main(string[] args)
{
try
{
string ComputerName = "localhost";
ManagementScope Scope;
if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
{
ConnectionOptions Conn = new ConnectionOptions();
Conn.Username = "";
Conn.Password = "";
Conn.Authority = "ntlmdomain:DOMAIN";
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
}
else
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
Scope.Connect();
string Drive= "c:";
//look how the \ char is escaped.
string Path="\\\\FolderName\\\\";
UInt64 FolderSize = 0;
ObjectQuery Query = new ObjectQuery(string.Format("SELECT * FROM CIM_DataFile Where Drive='{0}' AND Path='{1}' ", Drive, Path));
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
foreach (ManagementObject WmiObject in Searcher.Get())
{
Console.WriteLine("{0}", (string)WmiObject["FileName"]);// String
FolderSize +=(UInt64)WmiObject["FileSize"];
}
Console.WriteLine("{0,-35} {1,-40}", "Folder Size", FolderSize.ToString("N"));
}
catch (Exception e)
{
Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
}
Console.WriteLine("Press Enter to exit");
Console.Read();
}
}
}
这篇关于如何查询远程计算机中的文件夹大小通过WMI和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!