【icehawk】:
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);
}
【zswang】:
public long DirectorySize(string strDirectory)
{
if (!Directory.Exists(strDirectory)) return 0;
long vReturn = 0;
foreach (string vFile in Directory.GetFiles(strDirectory))
{
FileInfo vFileInfo = new FileInfo(vFile);
vReturn += vFileInfo.Length;
}
foreach (string vDirectory in Directory.GetDirectories(strDirectory))
vReturn += DirectorySize(vDirectory);
return vReturn;
}
【popeye627】:
/// <summary>
/// 计算一个目录的大小
/// </summary>
/// <param name="di">指定目录</param>
/// <param name="includeSubDir">是否包含子目录</param>
/// <returns></returns>
private long CalculateDirSize(DirectoryInfo di, bool includeSubDir)
{
long totalSize = 0;
// 检查所有(直接)包含的文件
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
totalSize += file.Length;
}
// 检查所有子目录,如果includeSubDir参数为true
if (includeSubDir)
{
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
totalSize += CalculateDirSize(dir, includeSubDir);
}
}
return totalSize;
}
【xvting】:
public class NDirectory
{
public static long GetLength(string p_strDirName)
{
#region 参数check
if (!Directory.Exists(p_strDirName)) return 0;
#endregion
#region 変量
string[] files = null;
string[] folders = null;
long length = 0;
#endregion
//文件計算
files = Directory.GetFiles(p_strDirName);
if (files != null)
{
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
length += fileInfo.Length;
}
}
//文件夹計算
folders = Directory.GetDirectories(p_strDirName);
if (folders != null)
{
foreach (string folder in folders)
{
length += NDirectory.GetLength(folder);
}
}
return length;
}
}
【wangchao1982】:
这个是网上搜来的,给一个正确的路径就肯定好用了(命令控制台)我测试过了
public static long GetDirectoryLength(string dirPath)
{
//判断给定的路径是否存在,如果不存在则退出
if (!Directory.Exists(dirPath))
return 0;
long len = 0;
DirectoryInfo di = new DirectoryInfo(dirPath);
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
}
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
return len;
}
.谢谢各位热心的朋友,所以我也不藏私,把自己刚刚调试好的帖到下面,也好用.虽然不如上面的简捷,但是思路也是很清晰.希望能给朋友们另一个思考方式.
static long fileLength = 0;
static void Main(string[] args)
{
string[] str = Directory.GetFileSystemEntries(demo);
foreach (string s in str)
{
fileLength += FileSize(s);
}
//输出文件大小(以M为单位)
Console.WriteLine(fileLength /(1024 * 1024));
}
//获取文件大小的递归方法
public static long FileSize(string filePath)
{
long temp = 0;
//判断filePath是文件还是文件夹
if (File.Exists(filePath) == false)
{
//如果是文件夹,则继续递归
string[] str1 = Directory.GetFileSystemEntries(filePath);
foreach (string s1 in str1)
{
temp += FileSize(s1);
}
}
//如果是文件
else
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}
return temp;
}
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);
}
【zswang】:
public long DirectorySize(string strDirectory)
{
if (!Directory.Exists(strDirectory)) return 0;
long vReturn = 0;
foreach (string vFile in Directory.GetFiles(strDirectory))
{
FileInfo vFileInfo = new FileInfo(vFile);
vReturn += vFileInfo.Length;
}
foreach (string vDirectory in Directory.GetDirectories(strDirectory))
vReturn += DirectorySize(vDirectory);
return vReturn;
}
【popeye627】:
/// <summary>
/// 计算一个目录的大小
/// </summary>
/// <param name="di">指定目录</param>
/// <param name="includeSubDir">是否包含子目录</param>
/// <returns></returns>
private long CalculateDirSize(DirectoryInfo di, bool includeSubDir)
{
long totalSize = 0;
// 检查所有(直接)包含的文件
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
totalSize += file.Length;
}
// 检查所有子目录,如果includeSubDir参数为true
if (includeSubDir)
{
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
totalSize += CalculateDirSize(dir, includeSubDir);
}
}
return totalSize;
}
【xvting】:
public class NDirectory
{
public static long GetLength(string p_strDirName)
{
#region 参数check
if (!Directory.Exists(p_strDirName)) return 0;
#endregion
#region 変量
string[] files = null;
string[] folders = null;
long length = 0;
#endregion
//文件計算
files = Directory.GetFiles(p_strDirName);
if (files != null)
{
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
length += fileInfo.Length;
}
}
//文件夹計算
folders = Directory.GetDirectories(p_strDirName);
if (folders != null)
{
foreach (string folder in folders)
{
length += NDirectory.GetLength(folder);
}
}
return length;
}
}
【wangchao1982】:
这个是网上搜来的,给一个正确的路径就肯定好用了(命令控制台)我测试过了
public static long GetDirectoryLength(string dirPath)
{
//判断给定的路径是否存在,如果不存在则退出
if (!Directory.Exists(dirPath))
return 0;
long len = 0;
DirectoryInfo di = new DirectoryInfo(dirPath);
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
}
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
return len;
}
.谢谢各位热心的朋友,所以我也不藏私,把自己刚刚调试好的帖到下面,也好用.虽然不如上面的简捷,但是思路也是很清晰.希望能给朋友们另一个思考方式.
static long fileLength = 0;
static void Main(string[] args)
{
string[] str = Directory.GetFileSystemEntries(demo);
foreach (string s in str)
{
fileLength += FileSize(s);
}
//输出文件大小(以M为单位)
Console.WriteLine(fileLength /(1024 * 1024));
}
//获取文件大小的递归方法
public static long FileSize(string filePath)
{
long temp = 0;
//判断filePath是文件还是文件夹
if (File.Exists(filePath) == false)
{
//如果是文件夹,则继续递归
string[] str1 = Directory.GetFileSystemEntries(filePath);
foreach (string s1 in str1)
{
temp += FileSize(s1);
}
}
//如果是文件
else
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}
return temp;
}