private long GetDirectorySizeMethod1(string directory)
{
  long directorySize = 0;
  DirectoryInfo di = new DirectoryInfo(directory);
  if (!di.Exists)
  {
    Console.WriteLine("Directory {0} is not exist!", directory);
    return 0;
  }
  foreach (FileInfo fi in di.GetFiles())
  {
  directorySize += fi.Length;
  }
  DirectoryInfo[] dirs = di.GetDirectories();
  foreach (DirectoryInfo sondir in dirs)
  {
    directorySize += GetDirectorySizeMethod1(sondir.FullName);
  }
  return directorySize;
}
private long GetDirectorySizeMethod2(string directory)
{
  long directorySize = 0;
  if (File.Exists(directory))
  {
    FileInfo fi = new FileInfo(directory);
    return fi.Length;
  }
  else
  {
    string[] strs = Directory.GetFileSystemEntries(directory);
    foreach (string str in strs)
    {
      directorySize += GetDirectorySizeMethod2(str);
    }
  }
  return directorySize;
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
  • 2022-01-01
  • 2021-05-25
  • 2022-12-23
  • 2021-08-20
相关资源
相似解决方案