我想知道是否某个任意路径引用了本地文件系统对象,而不是位于网络共享位置或可移动驱动器上。有没有办法在.NET中做到这一点?如何检查路径是否指向本地文件对象?
PS。换句话说,如果我有硬盘驱动器C:和d:和驱动器E:是DVD驱动器或USB闪存驱动器,然后:
下面的路径将是本地:
C:\Windows
D:\My Files\File.exe
而以下路径将不会:
E:\File on usb stick.txt
\\computer\file.ext
我想知道是否某个任意路径引用了本地文件系统对象,而不是位于网络共享位置或可移动驱动器上。有没有办法在.NET中做到这一点?如何检查路径是否指向本地文件对象?
PS。换句话说,如果我有硬盘驱动器C:和d:和驱动器E:是DVD驱动器或USB闪存驱动器,然后:
下面的路径将是本地:
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 - 枚举描述:
DriveType属性指示驱动器是否是以下任一项:CDRom, 固定,未知,网络,NoRootDirectory,Ram,可移动或未知。
好的,这是接近可测试的。您忘记提及如何使用路径中的驱动器号来初始化DriveInfo构造函数。这东西:http://msdn.microsoft.com/en-us/library/system.io.driveinfo.driveinfo.aspx – ahmd0
@ ahmd0我正在使用DriveInfo的静态方法 - 不需要构造函数 –
@ ahmd0只是使用do a allDrives.First(x => x.Name ==“D”)。DriveType == DriveType.Fixed已经从路径中提取了驱动器号来测试 –
DriveInfo类将告诉您它来自驱动器号的驱动器的类型。 – PhoenixReborn
这个问题似乎已经在[此线索]有了答案[1] [1]:http://stackoverflow.com/questions/2455348/check-whether-a-folder-is-a - 本地或网络资源网 – Brennan