.NET 磁盘空间 文件夹大小

//获取文件的总大小

public 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;
}

//下面就获取磁盘剩余的大小
public long GetDiskFreeSpace(string diskname)
{DriveInfo drive = new DriveInfo(diskname);
long freeSpace = drive.AvailableFreeSpace;
}


//下面就获取磁盘的大小
public  long GetHardDiskSpace(string str_HardDiskName)
  {
  long totalSize= new long();
  str_HardDiskName=str_HardDiskName +"://";
  System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
  foreach (System.IO.DriveInfo drive in drives)
  {
  if (drive.Name == str_HardDiskName)
  {
  totalSize = drive.TotalSize;
  }
  }
  return totalSize;
  }

你可能感兴趣的:(.net,String,磁盘)