检查Path是文件还是目录的更好方法是什么?

时间:2009-09-08 17:24:37

标签: c# .net file path directory

我正在处理TreeView个目录和文件。用户可以选择文件或目录,然后使用它执行某些操作。这要求我有一个方法,根据用户的选择执行不同的操作。

目前我正在做这样的事情来确定路径是文件还是目录:

bool bIsFile = false;
bool bIsDirectory = false;

try
{
    string[] subfolders = Directory.GetDirectories(strFilePath);

    bIsDirectory = true;
    bIsFile = false;
}
catch(System.IO.IOException)
{
    bIsFolder = false;
    bIsFile = true;
}

我不禁觉得有更好的方法可以做到这一点!我希望找到一个标准的.NET方法来处理这个问题,但我无法做到这一点。是否存在这样的方法,如果不存在,确定路径是文件还是目录的最简单方法是什么?

23 个答案:

答案 0 :(得分:527)

来自How to tell if path is file or directory

// get the file attributes for file or directory
FileAttributes attr = File.GetAttributes(@"c:\Temp");

//detect whether its a directory or file
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
    MessageBox.Show("Its a directory");
else
    MessageBox.Show("Its a file");

.NET 4.0 +

的更新

根据下面的评论,如果您使用的是.NET 4.0或更高版本(并且最高性能并不重要),您可以更清晰地编写代码:

// get the file attributes for file or directory
FileAttributes attr = File.GetAttributes(@"c:\Temp");

if (attr.HasFlag(FileAttributes.Directory))
    MessageBox.Show("Its a directory");
else
    MessageBox.Show("Its a file");

答案 1 :(得分:219)

如何使用这些?

File.Exists();
Directory.Exists();

答案 2 :(得分:19)

如果路径是目录或文件,只有这一行:

File.GetAttributes(data.Path).HasFlag(FileAttributes.Directory)

答案 3 :(得分:8)

这是我的:

    bool IsPathDirectory(string path)
    {
        if (path == null) throw new ArgumentNullException("path");
        path = path.Trim();

        if (Directory.Exists(path)) 
            return true;

        if (File.Exists(path)) 
            return false;

        // neither file nor directory exists. guess intention

        // if has trailing slash then it's a directory
        if (new[] {"\\", "/"}.Any(x => path.EndsWith(x)))
            return true; // ends with slash

        // if has extension then its a file; directory otherwise
        return string.IsNullOrWhiteSpace(Path.GetExtension(path));
    }

这与其他人的答案相似,但并不完全相同。

答案 4 :(得分:6)

作为Directory.Exists()的替代方法,您可以使用File.GetAttributes()方法获取文件或目录的属性,因此您可以创建一个这样的帮助方法:

private static bool IsDirectory(string path)
{
    System.IO.FileAttributes fa = System.IO.File.GetAttributes(path);
    return (fa & FileAttributes.Directory) != 0;
}

在填充包含项目的其他元数据的控件时,您还可以考虑将对象添加到TreeView控件的tag属性。例如,您可以为文件添加FileInfo对象,为目录添加DirectoryInfo对象,然后在tag属性中测试项类型,以便在单击项目时进行额外的系统调用以获取该数据。

答案 5 :(得分:5)

鉴于Exists和Attributes属性的行为,这是我能想到的最好的结果:

using System.IO;

public static class FileSystemInfoExtensions
{
    /// <summary>
    /// Checks whether a FileInfo or DirectoryInfo object is a directory, or intended to be a directory.
    /// </summary>
    /// <param name="fileSystemInfo"></param>
    /// <returns></returns>
    public static bool IsDirectory(this FileSystemInfo fileSystemInfo)
    {
        if (fileSystemInfo == null)
        {
            return false;
        }

        if ((int)fileSystemInfo.Attributes != -1)
        {
            // if attributes are initialized check the directory flag
            return fileSystemInfo.Attributes.HasFlag(FileAttributes.Directory);
        }

        // If we get here the file probably doesn't exist yet.  The best we can do is 
        // try to judge intent.  Because directories can have extensions and files
        // can lack them, we can't rely on filename.
        // 
        // We can reasonably assume that if the path doesn't exist yet and 
        // FileSystemInfo is a DirectoryInfo, a directory is intended.  FileInfo can 
        // make a directory, but it would be a bizarre code path.

        return fileSystemInfo is DirectoryInfo;
    }
}

以下是测试方法:

    [TestMethod]
    public void IsDirectoryTest()
    {
        // non-existing file, FileAttributes not conclusive, rely on type of FileSystemInfo
        const string nonExistentFile = @"C:\TotallyFakeFile.exe";

        var nonExistentFileDirectoryInfo = new DirectoryInfo(nonExistentFile);
        Assert.IsTrue(nonExistentFileDirectoryInfo.IsDirectory());

        var nonExistentFileFileInfo = new FileInfo(nonExistentFile);
        Assert.IsFalse(nonExistentFileFileInfo.IsDirectory());

        // non-existing directory, FileAttributes not conclusive, rely on type of FileSystemInfo
        const string nonExistentDirectory = @"C:\FakeDirectory";

        var nonExistentDirectoryInfo = new DirectoryInfo(nonExistentDirectory);
        Assert.IsTrue(nonExistentDirectoryInfo.IsDirectory());

        var nonExistentFileInfo = new FileInfo(nonExistentDirectory);
        Assert.IsFalse(nonExistentFileInfo.IsDirectory());

        // Existing, rely on FileAttributes
        const string existingDirectory = @"C:\Windows";

        var existingDirectoryInfo = new DirectoryInfo(existingDirectory);
        Assert.IsTrue(existingDirectoryInfo.IsDirectory());

        var existingDirectoryFileInfo = new FileInfo(existingDirectory);
        Assert.IsTrue(existingDirectoryFileInfo.IsDirectory());

        // Existing, rely on FileAttributes
        const string existingFile = @"C:\Windows\notepad.exe";

        var existingFileDirectoryInfo = new DirectoryInfo(existingFile);
        Assert.IsFalse(existingFileDirectoryInfo.IsDirectory());

        var existingFileFileInfo = new FileInfo(existingFile);
        Assert.IsFalse(existingFileFileInfo.IsDirectory());
    }

答案 6 :(得分:4)

最准确的方法是使用shlwapi.dll中的一些互操作代码

[DllImport(SHLWAPI, CharSet = CharSet.Unicode)]
[return: MarshalAsAttribute(UnmanagedType.Bool)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool PathIsDirectory([MarshalAsAttribute(UnmanagedType.LPWStr), In] string pszPath);

然后你会这样称呼它:

#region IsDirectory
/// <summary>
/// Verifies that a path is a valid directory.
/// </summary>
/// <param name="path">The path to verify.</param>
/// <returns><see langword="true"/> if the path is a valid directory; 
/// otherwise, <see langword="false"/>.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// <para><paramref name="path"/> is <see langword="null"/>.</para>
/// </exception>
/// <exception cref="T:System.ArgumentException">
/// <para><paramref name="path"/> is <see cref="F:System.String.Empty">String.Empty</see>.</para>
/// </exception>
public static bool IsDirectory(string path)
{
    return PathIsDirectory(path);
}

答案 7 :(得分:3)

在结合其他答案的建议后,我意识到我提出了与Ronnie Overby's answer相同的事情。以下是一些测试,指出需要考虑的一些事项:

  1. 文件夹可以包含&#34;扩展程序&#34;:C:\Temp\folder_with.dot
  2. 文件不能以目录分隔符(斜杠)结束
  3. 技术上有两个特定于平台的目录分隔符 - 即或者可能不是是斜杠(Path.DirectorySeparatorCharPath.AltDirectorySeparatorChar
  4. 测试(Linqpad)

    var paths = new[] {
        // exists
        @"C:\Temp\dir_test\folder_is_a_dir",
        @"C:\Temp\dir_test\is_a_dir_trailing_slash\",
        @"C:\Temp\dir_test\existing_folder_with.ext",
        @"C:\Temp\dir_test\file_thats_not_a_dir",
        @"C:\Temp\dir_test\notadir.txt",
        // doesn't exist
        @"C:\Temp\dir_test\dne_folder_is_a_dir",
        @"C:\Temp\dir_test\dne_folder_trailing_slash\",
        @"C:\Temp\dir_test\non_existing_folder_with.ext",
        @"C:\Temp\dir_test\dne_file_thats_not_a_dir",
        @"C:\Temp\dir_test\dne_notadir.txt",        
    };
    
    foreach(var path in paths) {
        IsFolder(path/*, false*/).Dump(path);
    }
    

    结果

    C:\Temp\dir_test\folder_is_a_dir
      True 
    C:\Temp\dir_test\is_a_dir_trailing_slash\
      True 
    C:\Temp\dir_test\existing_folder_with.ext
      True 
    C:\Temp\dir_test\file_thats_not_a_dir
      False 
    C:\Temp\dir_test\notadir.txt
      False 
    C:\Temp\dir_test\dne_folder_is_a_dir
      True 
    C:\Temp\dir_test\dne_folder_trailing_slash\
      True 
    C:\Temp\dir_test\non_existing_folder_with.ext
      False (this is the weird one)
    C:\Temp\dir_test\dne_file_thats_not_a_dir
      True 
    C:\Temp\dir_test\dne_notadir.txt
      False 
    

    方法

    /// <summary>
    /// Whether the <paramref name="path"/> is a folder (existing or not); 
    /// optionally assume that if it doesn't "look like" a file then it's a directory.
    /// </summary>
    /// <param name="path">Path to check</param>
    /// <param name="assumeDneLookAlike">If the <paramref name="path"/> doesn't exist, does it at least look like a directory name?  As in, it doesn't look like a file.</param>
    /// <returns><c>True</c> if a folder/directory, <c>false</c> if not.</returns>
    public static bool IsFolder(string path, bool assumeDneLookAlike = true)
    {
        // https://stackoverflow.com/questions/1395205/better-way-to-check-if-path-is-a-file-or-a-directory
        // turns out to be about the same as https://stackoverflow.com/a/19596821/1037948
    
        // check in order of verisimilitude
    
        // exists or ends with a directory separator -- files cannot end with directory separator, right?
        if (Directory.Exists(path)
            // use system values rather than assume slashes
            || path.EndsWith("" + Path.DirectorySeparatorChar)
            || path.EndsWith("" + Path.AltDirectorySeparatorChar))
            return true;
    
        // if we know for sure that it's an actual file...
        if (File.Exists(path))
            return false;
    
        // if it has an extension it should be a file, so vice versa
        // although technically directories can have extensions...
        if (!Path.HasExtension(path) && assumeDneLookAlike)
            return true;
    
        // only works for existing files, kinda redundant with `.Exists` above
        //if( File.GetAttributes(path).HasFlag(FileAttributes.Directory) ) ...; 
    
        // no idea -- could return an 'indeterminate' value (nullable bool)
        // or assume that if we don't know then it's not a folder
        return false;
    }
    

答案 8 :(得分:2)

以下是我们使用的内容:

using System;

using System.IO;

namespace crmachine.CommonClasses
{

  public static class CRMPath
  {

    public static bool IsDirectory(string path)
    {
      if (path == null)
      {
        throw new ArgumentNullException("path");
      }

      string reason;
      if (!IsValidPathString(path, out reason))
      {
        throw new ArgumentException(reason);
      }

      if (!(Directory.Exists(path) || File.Exists(path)))
      {
        throw new InvalidOperationException(string.Format("Could not find a part of the path '{0}'",path));
      }

      return (new System.IO.FileInfo(path).Attributes & FileAttributes.Directory) == FileAttributes.Directory;
    } 

    public static bool IsValidPathString(string pathStringToTest, out string reasonForError)
    {
      reasonForError = "";
      if (string.IsNullOrWhiteSpace(pathStringToTest))
      {
        reasonForError = "Path is Null or Whitespace.";
        return false;
      }
      if (pathStringToTest.Length > CRMConst.MAXPATH) // MAXPATH == 260
      {
        reasonForError = "Length of path exceeds MAXPATH.";
        return false;
      }
      if (PathContainsInvalidCharacters(pathStringToTest))
      {
        reasonForError = "Path contains invalid path characters.";
        return false;
      }
      if (pathStringToTest == ":")
      {
        reasonForError = "Path consists of only a volume designator.";
        return false;
      }
      if (pathStringToTest[0] == ':')
      {
        reasonForError = "Path begins with a volume designator.";
        return false;
      }

      if (pathStringToTest.Contains(":") && pathStringToTest.IndexOf(':') != 1)
      {
        reasonForError = "Path contains a volume designator that is not part of a drive label.";
        return false;
      }
      return true;
    }

    public static bool PathContainsInvalidCharacters(string path)
    {
      if (path == null)
      {
        throw new ArgumentNullException("path");
      }

      bool containedInvalidCharacters = false;

      for (int i = 0; i < path.Length; i++)
      {
        int n = path[i];
        if (
            (n == 0x22) || // "
            (n == 0x3c) || // <
            (n == 0x3e) || // >
            (n == 0x7c) || // |
            (n  < 0x20)    // the control characters
          )
        {
          containedInvalidCharacters = true;
        }
      }

      return containedInvalidCharacters;
    }


    public static bool FilenameContainsInvalidCharacters(string filename)
    {
      if (filename == null)
      {
        throw new ArgumentNullException("filename");
      }

      bool containedInvalidCharacters = false;

      for (int i = 0; i < filename.Length; i++)
      {
        int n = filename[i];
        if (
            (n == 0x22) || // "
            (n == 0x3c) || // <
            (n == 0x3e) || // >
            (n == 0x7c) || // |
            (n == 0x3a) || // : 
            (n == 0x2a) || // * 
            (n == 0x3f) || // ? 
            (n == 0x5c) || // \ 
            (n == 0x2f) || // /
            (n  < 0x20)    // the control characters
          )
        {
          containedInvalidCharacters = true;
        }
      }

      return containedInvalidCharacters;
    }

  }

}

答案 9 :(得分:2)

我在遇到类似问题时遇到过此问题,但我需要检查路径是否适用于文件或文件夹,当该文件或文件夹可能实际不存在时。上面的答案中有一些评论提到它们不适用于这种情况。我找到了一个解决方案(我使用VB.NET,但你可以转换,如果你需要)似乎对我有用:

Dim path As String = "myFakeFolder\ThisDoesNotExist\"
Dim bIsFolder As Boolean = (IO.Path.GetExtension(path) = "")
'returns True

Dim path As String = "myFakeFolder\ThisDoesNotExist\File.jpg"
Dim bIsFolder As Boolean = (IO.Path.GetExtension(path) = "")
'returns False

希望这对某人有帮助!

答案 10 :(得分:2)

我知道,在游戏后期太晚了,但我想我还是会分享这个。如果您只是将路径作为字符串使用,那么很容易理解这一点:

private bool IsFolder(string ThePath)
{
    string BS = Path.DirectorySeparatorChar.ToString();
    return Path.GetDirectoryName(ThePath) == ThePath.TrimEnd(BS.ToCharArray());
}

例如: ThePath == "C:\SomeFolder\File1.txt"最终会成为这样:

return "C:\SomeFolder" == "C:\SomeFolder\File1.txt" (FALSE)

另一个例子: ThePath == "C:\SomeFolder\"最终会成为这样:

return "C:\SomeFolder" == "C:\SomeFolder" (TRUE)

这也可以在没有反斜杠的情况下工作: ThePath == "C:\SomeFolder"最终会成为这样:

return "C:\SomeFolder" == "C:\SomeFolder" (TRUE)

请记住,这只适用于路径本身,而不是路径与“物理磁盘”之间的关系...所以它无法告诉您路径/文件是否存在或类似的东西,但它确实可以告诉你路径是文件夹还是文件......

答案 11 :(得分:1)

    `public bool IsDirectory( string path ) {
        return string.IsNullOrEmpty( Path.GetFileName( path ) ) || Directory.Exists( path );
    }`

检查路径文件名是否为空字符串,或者目录是否存在。这样你就不会出现文件属性错误,同时仍然为可能的存在失败提供冗余。

答案 12 :(得分:1)

如果要查找目录,包括标记为“hidden”和“system”的目录,请尝试此操作(需要.NET V4):

FileAttributes fa = File.GetAttributes(path);
if(fa.HasFlag(FileAttributes.Directory)) 

答案 13 :(得分:0)

using System;
using System.IO;
namespace FileOrDirectory
{
     class Program
     {
          public static string FileOrDirectory(string path)
          {
               if (File.Exists(path))
                    return "File";
               if (Directory.Exists(path))
                    return "Directory";
               return "Path Not Exists";
          }
          static void Main()
          {
               Console.WriteLine("Enter The Path:");
               string path = Console.ReadLine();
               Console.WriteLine(FileOrDirectory(path));
          }
     }
}

答案 14 :(得分:0)

我需要这个,帖子有帮助,这会把它降到一行,如果路径根本不是路径,它只返回并退出方法。它解决了上述所有问题,也不需要尾随斜杠。

if (!Directory.Exists(@"C:\folderName")) return;

答案 15 :(得分:0)

在这篇文章中使用所选答案,我查看了评论并给予了信任 @ŞafakGür,@ Anthony和@Quinn Wilson的信息位让我得到了我编写和测试的改进答案:

    /// <summary>
    /// Returns true if the path is a dir, false if it's a file and null if it's neither or doesn't exist.
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static bool? IsDirFile(this string path)
    {
        bool? result = null;

        if(Directory.Exists(path) || File.Exists(path))
        {
            // get the file attributes for file or directory
            var fileAttr = File.GetAttributes(path);

            if (fileAttr.HasFlag(FileAttributes.Directory))
                result = true;
            else
                result = false;
        }

        return result;
    }

答案 16 :(得分:0)

也许是UWP C#

public static async Task<IStorageItem> AsIStorageItemAsync(this string iStorageItemPath)
    {
        if (string.IsNullOrEmpty(iStorageItemPath)) return null;
        IStorageItem storageItem = null;
        try
        {
            storageItem = await StorageFolder.GetFolderFromPathAsync(iStorageItemPath);
            if (storageItem != null) return storageItem;
        } catch { }
        try
        {
            storageItem = await StorageFile.GetFileFromPathAsync(iStorageItemPath);
            if (storageItem != null) return storageItem;
        } catch { }
        return storageItem;
    }

答案 17 :(得分:0)

我知道,我参加聚会已经晚了10年。 我遇到的情况是,从某些属性中我可以接收文件名或完整的文件路径。如果没有提供路径,我必须通过附加另一个属性提供的“全局”目录路径来检查文件是否存在。

就我而言

var isFileName = System.IO.Path.GetFileName (str) == str;

成功了。 好的,这不是魔术,但这也许可以节省某人几分钟的时间。 由于这只是一个字符串解析,因此带点的Dir名称可能会带来误报...

答案 18 :(得分:0)

在这里参加聚会很晚,但是我发现Nullable<Boolean>的返回值非常丑陋-IsDirectory(string path)返回null并不等于不存在冗长的路径评论,因此我提出了以下建议:

public static class PathHelper
{
    /// <summary>
    /// Determines whether the given path refers to an existing file or directory on disk.
    /// </summary>
    /// <param name="path">The path to test.</param>
    /// <param name="isDirectory">When this method returns, contains true if the path was found to be an existing directory, false in all other scenarios.</param>
    /// <returns>true if the path exists; otherwise, false.</returns>
    /// <exception cref="ArgumentNullException">If <paramref name="path"/> is null.</exception>
    /// <exception cref="ArgumentException">If <paramref name="path"/> equals <see cref="string.Empty"/></exception>
    public static bool PathExists(string path, out bool isDirectory)
    {
        if (path == null) throw new ArgumentNullException(nameof(path));
        if (path == string.Empty) throw new ArgumentException("Value cannot be empty.", nameof(path));

        isDirectory = Directory.Exists(path);

        return isDirectory || File.Exists(path);
    }
}

此辅助方法写得很详细,简洁,足以在您初次阅读时理解其意图。

/// <summary>
/// Example usage of <see cref="PathExists(string, out bool)"/>
/// </summary>
public static void Usage()
{
    const string path = @"C:\dev";

    if (!PathHelper.PathExists(path, out var isDirectory))
        return;

    if (isDirectory)
    {
        // Do something with your directory
    }
    else
    {
        // Do something with your file
    }
}

答案 19 :(得分:0)

只需添加一个边缘盒-“文件夹选择”。在路径中

在我的应用程序中,我获得了最近打开的传递给我的路径,其中有些具有“文件夹选择”。最后。

某些FileOpenDialogs和WinMerge添加“文件夹选择”。路径(是真的)。​​

Dialog showing "Folder Selection." getting added to path

但是在Windows OS的“文件夹选择”下。不是建议使用的文件或文件夹名称(例如,永远不要这样做-握拳)。 如此处所述:http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx

请勿在文件名或目录名后加空格或句点。尽管基础文件系统可能支持此类名称,但Windows Shell和用户界面不支持。但是,可以将句点指定为名称的第一个字符。例如“ .temp”。

因此,在“文件夹选择”中。 不应该使用,可以使用。 (太棒了)。

足够的解释-我的代码(我非常喜欢枚举):

public static class Utility
{
    public enum ePathType
    {
        ePathType_Unknown = 0,
        ePathType_ExistingFile = 1,
        ePathType_ExistingFolder = 2,
        ePathType_ExistingFolder_FolderSelectionAdded = 3,
    }

    public static ePathType GetPathType(string path)
    {
        if (File.Exists(path) == true) { return ePathType.ePathType_ExistingFile; }
        if (Directory.Exists(path) == true) { return ePathType.ePathType_ExistingFolder; }

        if (path.EndsWith("Folder Selection.") == true)
        {
            // Test the path again without "Folder Selection."
            path = path.Replace("\\Folder Selection.", "");
            if (Directory.Exists(path) == true)
            {
                // Could return ePathType_ExistingFolder, but prefer to let the caller known their path has text to remove...
                return ePathType.ePathType_ExistingFolder_FolderSelectionAdded;
            }
        }

        return ePathType.ePathType_Unknown;
    }
}

答案 20 :(得分:0)

我使用以下内容,它还测试扩展,这意味着如果提供的路径是文件但文件不存在,它可以用于测试。

private static bool isDirectory(string path)
{
    bool result = true;
    System.IO.FileInfo fileTest = new System.IO.FileInfo(path);
    if (fileTest.Exists == true)
    {
        result = false;
    }
    else
    {
        if (fileTest.Extension != "")
        {
            result = false;
        }
    }
    return result;
}

答案 21 :(得分:-4)

这不会起作用吗?

var isFile = Regex.IsMatch(path, @"\w{1,}\.\w{1,}$");

答案 22 :(得分:-5)

这是使用DirectoryInfo获取属性

Connection con = null;
Statement st = null;
ResultSet rs = null;

...
try {
    if (rs != null)
        rs.close();
    if (st != null) 
        st.close();
    if (con != null)
        con.close();
} catch (Exception e) {
    System.out.println("No");
}

如果您尝试通过DirectoryInfo尝试创建TreeView或读取TreeView

,这将有效