从路径字符串或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();
一个小字符串解析没有错:-)
FullPath.SubString(0,1);
您可以使用以下代码获取系统中的所有驱动器:
foreach (DriveInfo objDrive in DriveInfo.GetDrives()) { Response.Write("Drive Type : " + objDrive.Name); Response.Write("Drive Type : " + objDrive.DriveType.ToString()); Response.Write("Available Free Space : " + objDrive.AvailableFreeSpace.ToString() + "(bytes)"); Response.Write("Drive Format : " + objDrive.DriveFormat); Response.Write("Total Free Space : " + objDrive.TotalFreeSpace.ToString() + "(bytes)"); Response.Write("Total Size : " + objDrive.TotalSize.ToString() + "(bytes)"); Response.Write("Volume Label : " + objDrive.VolumeLabel); Response.Write(""); }
- 在assembly信息中动态显示当前年份
- 洋葱架构,工作单元和通用存储库模式
- 当服务器和客户端在同一台计算机上时,收听广播
- 从PInvoke返回一个字符串?
- 使用iTextSharp将图像转换为PDF保留剪切路径
- 找不到方法:’Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)’
- 如何更改hover时gridview内可用控件的工具提示的背景颜色
- 如何在SelectedIndexChanged事件之前检测TabControl中标签页的更改?
- Request.IsAuthenticated总是错误的