如何检查路径是否引用本地文件对象?

时间:2013-10-16 22:29:34

标签: c# .net filesystems

我试图找出一些任意路径是指本地文件系统对象还是位于网络共享或可移动驱动器上的路径。在.NET中有没有办法做到这一点?

PS。换句话说,如果我有硬盘驱动器C:和D:并且驱动器E:是DVD驱动器或USB闪存驱动器,那么:

以下路径将是本地路径:

C:\Windows
D:\My Files\File.exe

以下路径不会:

E:\File on usb stick.txt
\\computer\file.ext

1 个答案:

答案 0 :(得分:2)

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,可移动或未知。