2

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.

I tried Win32_Directory , CMI_DataFile but not able to find the desired answer. Please help!!

Anoop
  • 41
  • 1
  • 4

1 Answers1

3

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

}
Helen
  • 87,344
  • 17
  • 243
  • 314
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • I am not getting folder size by using the above code. It just gives me the size of the first file in the folder/subfolder. – Anoop Oct 22 '12 at 08:54
  • Please help as I need this urgently – Anoop Oct 22 '12 at 08:55
  • 1
    Please check these suggestions 1. Are you modified the code in any way? 2. Are you aware which this code is not recursive? – RRUZ Oct 22 '12 at 14:15