您当前的位置: 首页 >  .net

在asp.net(C#)中怎么获得一个目录的大小?

发布时间:2004-10-25 19:01:00 ,浏览量:0

作者:靶子&MSDN       出处:msdn 本方法来自以下文档,有问题多翻帮助 ;) ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfsystemiodirectoryclasstopic.htm ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemiodirectoryclasstopic.htm 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 command line."); } else { DirectoryInfo d = new DirectoryInfo(args[0]); Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, DirSize(d)); } } }
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    108697博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0459s