当前位置:首页  »  编程知识  »  C#.NET 怎么获得目录的大小?

  • C#.NET 怎么获得目录的大小?

  • 【编者按】:网络教育吧编程知识为您提供C#.NET 怎么获得目录的大小?参考,解决您在C#.NET 怎么获得目录的大小?学习中工作中的难题,参考学习。

    public static long DirSize(DirectoryInfo d)
    {
     long Size = 0;
     // Add file sizes.
     FileInfo[] fis = d.GetFiles();
     foreach (FileInfo fi in fis)
     {
     Size += fi.Length;
     }
     // Add subdirectory sizes.
     DirectoryInfo[] dis = d.GetDirectories();
     foreach (DirectoryInfo di in dis)
     {
      Size += DirSize(di);
     }
     return(Size);
    }

    应用示例:

    // The following example calculates the size of a directory
    // and its subdirectories, if any, and displays the total size
    // in bytes.

    using System;
    using System.IO;

    public class ShowDirSize
    {
     public static long DirSize(DirectoryInfo d)
     {
      long Size = 0;
      // Add file sizes.
      FileInfo[] fis = d.GetFiles();
      foreach (FileInfo fi in fis)
      {
       Size += fi.Length;
      }
      // Add subdirectory sizes.
      DirectoryInfo[] dis = d.GetDirectories();
      foreach (DirectoryInfo di in dis)
      {
       Size += DirSize(di);
      }
      return(Size);
      }
      public static void Main(string[] args)
      {
       if (args.Length != 1)
       {
        Console.WriteLine("You must provide a directory argument at the mand line.");
       }
       else
       {
        DirectoryInfo d = new DirectoryInfo(args[0]);
        Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, DirSize(d));
       }
     }
    }
     

    全国精品课程资源库
  • 友情链接