我正在处理目录和文件的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方法来处理这个问题,但我还没有能够这样做。是否存在这样的方法,如果不存在,确定路径是文件还是目录的最直接的方法是什么?
很晚才到这里,但我发现Nullable<Boolean>返回值相当难看- IsDirectory(字符串路径)返回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);
}
}
这个helper方法写得足够冗长和简洁,以便在第一次阅读时就能理解其意图。
/// <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
}
}
很晚才到这里,但我发现Nullable<Boolean>返回值相当难看- IsDirectory(字符串路径)返回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);
}
}
这个helper方法写得足够冗长和简洁,以便在第一次阅读时就能理解其意图。
/// <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
}
}
我知道这游戏太迟了,但我还是想分享这个。如果你只是将路径作为字符串来处理,这很容易:
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)
请记住,这只适用于路径本身,而不适用于路径和“物理磁盘”之间的关系……所以它不能告诉你路径/文件是否存在或类似的东西,但它肯定能告诉你路径是文件夹还是文件……