这可能是一个愚蠢的问题,那么问题来了:
除了解析FileInfo.FullPath字符串以获取驱动器号码,然后使用DriveInfo("c")等方法查看是否有足够的空间来写入此文件之外,还有没有从FileInfo获取驱动器号码的方法?
这可能是一个愚蠢的问题,那么问题来了:
除了解析FileInfo.FullPath字符串以获取驱动器号码,然后使用DriveInfo("c")等方法查看是否有足够的空间来写入此文件之外,还有没有从FileInfo获取驱动器号码的方法?
FileInfo f = new FileInfo(path);
string drive = Path.GetPathRoot(f.FullName);
这将返回"C:\"。那确实是唯一的另一种方法。
嗯,还有这个:
FileInfo file = new FileInfo(path);
DriveInfo drive = new DriveInfo(file.Directory.Root.FullName);
嘿,为什么不使用扩展方法呢?
public static DriveInfo GetDriveInfo(this FileInfo file)
{
return new DriveInfo(file.Directory.Root.FullName);
}
那么你可以直接这样做:
DriveInfo drive = new FileInfo(path).GetDriveInfo();
DriveInfo
调用 GetPathRoot()
,所以如果只想要驱动器字母,这是额外的开销。另一方面,很酷;直到现在我都不知道 DriveInfo
的存在。 - Still.Tonyforeach (DriveInfo objDrive in DriveInfo.GetDrives())
{
Response.Write("</br>Drive Type : " + objDrive.Name);
Response.Write("</br>Drive Type : " + objDrive.DriveType.ToString());
Response.Write("</br>Available Free Space : " + objDrive.AvailableFreeSpace.ToString() + "(bytes)");
Response.Write("</br>Drive Format : " + objDrive.DriveFormat);
Response.Write("</br>Total Free Space : " + objDrive.TotalFreeSpace.ToString() + "(bytes)");
Response.Write("</br>Total Size : " + objDrive.TotalSize.ToString() + "(bytes)");
Response.Write("</br>Volume Label : " + objDrive.VolumeLabel);
Response.Write("</br></br>");
}
对于一些字符串解析没什么问题 :-)
FullPath.Substring(0,1);