你可以使用
GetDirectoryName
,仅获取文件的目录路径:
using System.IO;
string directoryName = Path.GetDirectoryName(@"C:\myfolder\subfolder\mydoc.pdf");
// directoryName now contains "C:\myfolder\subfolder"
为了计算目录和子目录的大小,可以这样做:
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);
}