我试图找出某个任意路径是指本地文件系统对象还是位于网络共享或可移动驱动器上的路径。在 .NET 中有什么方法可以做到这一点吗?
附言。换句话说,如果我有硬盘 C: 和 D: 并且驱动器 E: 是 DVD 驱动器或 U 盘,那么:
以下路径将是本地路径:
C:\Windows
D:\My Files\File.exe
下面的路径不会:
E:\File on usb stick.txt
\\computer\file.ext
最佳答案
using System;
using System.IO;
namespace random
{
class Program
{
static void Main(string[] args)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
//TEST HERE
bool isFixed = allDrives.First(x=>x.Name == "D").DriveType == DriveType.Fixed
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
Console.Read();
}
}
}
}
您需要 DriveInfo 类,特别是 DriveType - 枚举说明:
The DriveType property indicates whether a drive is any of: CDRom, Fixed, Unknown, Network, NoRootDirectory, Ram, Removable, or Unknown.
关于c# - 如何检查路径是否引用本地文件对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19415092/