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

标签 c# .net filesystems

我试图找出某个任意路径是指本地文件系统对象还是位于网络共享或可移动驱动器上的路径。在 .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/

相关文章:

ruby - FileUtils 更新了吗? Ruby 中的问题

c# - 为什么需要声明

javascript - 为(服务器端)格式化数据(客户端)

c# - 在 C# 中选择单词部分

c# - ProtectedData 可以在多台计算机上工作吗?

c# - 将 Monotouch 与 Google .NET API 结合使用

PHP 检测文件系统编码/保存非拉丁文文件名的文件

node.js - 在没有 GridFS 的情况下将文件存储在 mongoDB 中

c# - 我们应该按什么顺序实现Asp.Net Mvc App

c# - 如何用c#写状态机?