2

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

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

以下路径将是本地路径:

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

以下路径不会:

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

1 回答 1

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、Fixed、Unknown、Network、NoRootDirectory、Ram、Removable 或 Unknown。

于 2013-10-16T22:47:58.940 回答