【问题标题】:How to query Folder Size in remote computer through WMI and C#如何通过 WMI 和 C# 查询远程计算机中的文件夹大小
【发布时间】:2012-10-17 23:45:10
【问题描述】:

如何通过 WMI 和 C# 查询远程计算机中的文件夹大小。 我需要通过 WMI 在远程系统的 C:\Users 中找到每个用户的文件夹大小。

我尝试过 Win32_Directory 、 CMI_DataFile 但无法找到所需的答案。 请帮忙!!

【问题讨论】:

    标签: c#-4.0 wmi wmi-query


    【解决方案1】:

    要使用 WMI 获取文件夹的大小,您必须使用 CIM_DataFile 类遍历文件,然后从 FileSize 属性中获取每个文件的大小。

    试试这个示例(这个代码不是递归的,我把这个任务留给你)。

    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();
            }
        }
    
    }
    

    【讨论】:

    • 我没有使用上面的代码获取文件夹大小。它只是给了我文件夹/子文件夹中第一个文件的大小。
    • 请帮忙,因为我急需这个
    • 请检查这些建议 1.您是否以任何方式修改了代码? 2.你知道这段代码不是递归的吗?
    猜你喜欢
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 2010-11-23
    相关资源
    最近更新 更多