用C#实现获取文件夹大小的源代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FileList
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine();
long length = GetDirectoryLength(@"D:\Folder\碟四");
Console.WriteLine(length.ToString("N0"));
Console.WriteLine();
long length2 = FileSize(@"D:\Folder\碟四");
Console.WriteLine(length2.ToString("N0"));
Console.WriteLine(DateTime.Now.Millisecond);
} public static long GetDirectoryLength(string dirPath)
{
//判断给定的路径是否存在,如果不存在则退出
if (!Directory.Exists(dirPath))
return ;
long len = ; //定义一个DirectoryInfo对象
DirectoryInfo di = new DirectoryInfo(dirPath); //通过GetFiles方法,获取di目录中的所有文件的大小
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
} //获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > )
{
for (int i = ; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
return len;
} //也是利用递归的思想,只不过是通过File类的Exits方法来判断
//所给路径中所对应的是否为文件
public static long FileSize(string filePath)
{
long temp = ; //判断当前路径所指向的是否为文件
if (File.Exists(filePath) == false)
{
string[] str1 = Directory.GetFileSystemEntries(filePath);
foreach (string s1 in str1)
{
temp += FileSize(s1);
}
}
else
{
//定义一个FileInfo对象,使之与filePath所指向的文件向关联,
//以获取其大小
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}
return temp;
}
}
}
用C#实现获取文件夹大小的源代码的更多相关文章
- Linux C++获取文件夹大小
项目中要计算指定文件夹的大小.百度查到这篇文章,https://my.oschina.net/Tsybius2014/blog/330628方法可行,运行正确. 拿到我们的项目中,却遇到一些问题:程序 ...
- 【VBS】获取文件夹大小
文件截图: 运行结果: 第一步:编写脚本 GetFloderSize.vbs 1 '获得文件夹的大小 by 王牌飞行员(https://www.cnblogs.com/KMould/p/1233481 ...
- python 获取文件夹大小
__author__ = 'bruce' import os from os.path import join,getsize def getdirsize(dir): size=0l for (ro ...
- c# 获取文件夹大小
private long GetDirectorySizeMethod1(string directory) { long directorySize = 0; DirectoryInfo di = ...
- C#获取文件和文件夹大小
代码如下: /// <summary> /// 获取文件夹大小 /// </summary> /// <param name="dirPath"> ...
- python3获取文件及文件夹大小
获取文件大小 os.path.getsize(file_path):file_path为文件路径 >>> import os >>> os.path.getsize ...
- python 获取文件和文件夹大小
1.os.path.getsize可以获取文件大小 >>> import os >>> file_name = 'E:\chengd\Cd.db' >> ...
- python获取文件及文件夹大小
Python3.3下测试通过 获取文件大小 使用os.path.getsize函数,参数是文件的路径 获取文件夹大小 import os from os.path import join, getsi ...
- android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位
android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位 public class FileSizeUtil { public static final int SIZETYPE_B ...
随机推荐
- E. You Are Given Some Strings...
E. You Are Given Some Strings... AC自动机 求一个串$t$中包含子串$s_{i}+s_{j}$的个数. 可以正反跑两遍AC自动机 正着跑,表示$s_{i}$结束,反正 ...
- es的调优
3.1.分片查询方式 当前的图片中有5个主分片,5个副本:这对于es的集群来说,这种配置是非常常见的: 但是问题来了,当我们的客户端做查询的时候,程序会向主分片发送请求还是副本发送请求? 还是说直接去 ...
- chrome flash 自动暂停问题
chrome flash 尺寸小于398*298时,只要宽和高某一个值小于对应值就会自动暂停,出现这个圆形的播放按钮.(估计是当广告处理了...) 将尺寸调大即可.
- 【洛谷P5018 对称二叉树】
话说这图也太大了吧 这题十分的简单,我们可以用两个指针指向左右两个对称的东西,然后比较就行了 复杂度O(n*logn) #include<bits/stdc++.h> using name ...
- goroutine 分析 协程的调度和执行顺序 并发写
package main import ( "fmt" "runtime" "sync" ) const N = 26 func main( ...
- python-笔记-内置函数
###内置函数 print(all([1,2,3,4]))判断可迭代的对象里面的值是否都为真 print(any([0,1,2,3,4]))判断可迭代的对象里面的值是否有一个为真 print(id(l ...
- st.getParameter() 和request.getAttribute() 区别 https://terryjs.iteye.com/blog/1317610
getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute. (1)request.getParameter() 取得是通过容 ...
- nginx windows安装基础
nginx在 window上运行需要1.17.3以上. 官方文件https://nginx.org/en/docs/windows.html nginx启动: 1:进入安装目录,双击nginx.exe ...
- mysql新建表
CREATE TABLE table( id int(20) not null auto_increment primary key, //auto_increment当为空时自动补全,注意,类型应该 ...
- ToolProvider.getSystemJavaCompiler()方法空指针的排坑
起因: 我在做一个编译Java代码的功能,基本写的差不多了,我就想把它打包部署到我服务器上跑一跑,但是这不做不知道,一做果然就出了问题.我在IDEA上跑一点问题都没有,但是打包成Jar后,后台就显示空 ...