0

Requirement: Get the size of a folder or a remote computer within a reasonable timeframe.

WMI takes way too long to enumerate a directory and then calculate the size of its content's.

I attempted the following code however it says that its not a valid path

String Folder = Row["Location"].ToString();
String[] Files = Directory.GetFiles(@"\\" + Folder.Remove(0, 2) + @"\c$" + Folder, "*", SearchOption.AllDirectories);
long TotalFolderSize = 0;
foreach (String File in Files)
{
    TotalFolderSize += new FileInfo(File).Length;
}

I'm not overly that great with PInvoke, still pretty new to C#. I saw that Kernel32.dll has some calls for files and disk but cannot find anything about folders or directories.

so the questions are

  1. Is there a API/Function in C# that can get the size of a directory over the network.

  2. Is there a PInvoke call that you can use to do this

  3. What is the best way to do this?

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
New Bee
  • 991
  • 1
  • 13
  • 24
  • 1
    tbh for network shares your approach is correct (get a folder and loop over the files). I'm not sure what your attempting on the 2nd line, but I would move that string manipulation and put it in a separate variable. – cjb110 Jul 23 '14 at 07:06
  • 1
    http://stackoverflow.com/questions/468119/whats-the-best-way-to-calculate-the-size-of-a-directory-in-net – Abdul Majid Jul 23 '14 at 07:08
  • As @AbdulMajid pointed out this question already asked some time ago - and nothing really new is available (for local/remote directories). Please make sure to read long answer about pitfalls of "file/folder size" in linked question. – Alexei Levenkov Jul 23 '14 at 07:24
  • As far as I can tell from our company network, the code itself works. Possibly the folder path is malformed? What is the exact content of `Row["Location"]`? – Jens H Jul 23 '14 at 07:45

2 Answers2

2

Use the Directory and FileInfo classes.

Code example and more information can be found here:

static long GetDirectorySize(string p)
{
    // 1.
    // Get array of all file names.
    string[] a = Directory.GetFiles(p, "*", SearchOption.AllDirectories);

    // 2.
    // Calculate total bytes of all files in a loop.
    long b = 0;
    foreach (string name in a)
    {
        // 3.
        // Use FileInfo to get length of each file.
        FileInfo info = new FileInfo(name);
        b += info.Length;
    }

    // 4.
    // Return total size
    return b;
}
Jens H
  • 4,590
  • 2
  • 25
  • 35
1

For a network share your approach is correct, as you don't have access to the underlying file system for any 'quicker' method. However your code has some issues, the followign shows the basics:

    string networkPath = @"\\sgb8532fh92\AppData\PCUWEB";

    DirectoryInfo di = new DirectoryInfo(networkPath);
    long totalSize = 0;

    foreach (FileInfo fi in di.GetFiles())
    {
        totalSize += fi.Length;
    }

    Console.WriteLine("Total Size: {0}Mb", totalSize / 1024 / 1024);
    Console.ReadLine();

Also note this will only return the size of the files in the folder, not any sub folder. For this I'd look at recursion.

cjb110
  • 1,310
  • 14
  • 30