获取文件大小
public static bool GetFileSize(string filePath, out long fileLength)
{
fileLength = -1;
if (File.Exists(filePath))
{
FileInfo fileInfo = new FileInfo(filePath);
fileLength = fileInfo.Length;
return true;
}
return false;
}
获取文件夹及子目录文件大小
public static bool GetDirectorySize(string directoryPath, out long directoryLength)
{
directoryLength= -1;
if (Directory.Exists(directoryPath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
foreach (var item in directoryInfo.GetFiles())
{
directoryLength += item.Length;
}
foreach (var item in directoryInfo.GetDirectories())
{
if (GetDirectorySize(item.FullName,out long dirLength))
{
directoryLength += dirLength;
}
}
return true;
}
return false;
}