C# 如何从包含通配符的路径中提取目录?
给定如下路径:C# 如何从包含通配符的路径中提取目录?,c#,.net,file,C#,.net,File,给定如下路径: C:\Temp\SomeDirectory\*.xml 我希望能够区分*.xml和C:\Temp\SomeDirectory 但是,我不希望一个没有尾部斜杠的目录的路径返回其父目录 这意味着我需要以下行为: // Wildcard paths return directory C:\Temp\SomeDirectory\*.csv -> C:\Temp\SomeDirectory // Trailing slash paths return full path C:\T
C:\Temp\SomeDirectory\*.xml
我希望能够区分*.xml
和C:\Temp\SomeDirectory
但是,我不希望一个没有尾部斜杠的目录的路径返回其父目录
这意味着我需要以下行为:
// Wildcard paths return directory
C:\Temp\SomeDirectory\*.csv -> C:\Temp\SomeDirectory
// Trailing slash paths return full path
C:\Temp\SomeDirectory\ -> C:\Temp\SomeDirectory
// Non-trailing slash paths to a directory return full path
C:\Temp\SomeDirectory -> C:\Temp\SomeDirectory
// Paths to a file return the directory
C:\Temp\SomeDirectory\SomeFileThatExists.csv -> C:\Temp\SomeDirectory
// Paths to a file without an extension (that exists) return the directory
C:\Temp\SomeDirectory\SomeFileThatExistsWithNoExt -> C:\Temp\SomeDirectory
// Paths to a non-existent path without a trailing slash are standard
// Either always clip the trailing part, or always leave it in
// (Can live with this one being C:\Temp\SomeDirectory)
C:\Temp\SomeDirectory\NonExistentObject -> C:\Temp\SomeDirectory\NonExistentObject
// Paths to a non-existent path with a trailing slash return the full path
C:\Temp\SomeDirectory\NonExistentObject\ -> C:\Temp\SomeDirectory\NonExistentObject
// Paths to a non-existent path with a file extension return the directory
C:\Temp\SomeDirectory\NonExistentFile.Ext -> C:\Temp\SomeDirectory
(如果返回值是否有尾随斜杠,我并不担心,尽管我在下面介绍的方法始终不返回尾随斜杠)
我当前的代码是这样的,并处理以下情况:
public string GetDirectory(string path)
{
try
{
var f = new FileInfo(path); // Throws if invalid path, e.g. wildcards
// Existent directory
if (Directory.Exists(path))
{
// Full path must be a directory, so return full path
// Ensure to add a trailing slash, as if it's missing it will return parent directory
return Path.GetDirectoryName(path + '/');
}
// Non-existent directory (or ambiguous path without an extension or trailing slash)
if (!System.IO.File.Exists(path) && String.IsNullOrEmpty(Path.GetExtension(path)))
{
// Path is to a non-existent file (without an extension) or to a non-existent directory.
// As the path does not exist we will standardise and treat it as a directory.
return Path.GetDirectoryName(path + '/');
}
// Path is to a file, return directory
return Path.GetDirectoryName(path);
}
catch (ArgumentException)
{
// For wildcards/invalid paths, return the directory
// This maps C:\Dir\*.csv to C:\Dir
// Also maps C:\Dir&*A*#$!@& to C:\
return Path.GetDirectoryName(path);
}
}
有没有更好的方法来实现这种行为,或者我的最终目标是能够从可能包含通配符的路径中获取“目录”?我认为问题在于路径方法只是字符串操作函数。我不相信他们真的会出去看看你是在看一个目录还是一个没有扩展名的文件
您需要结合使用目录或文件类来找出它是什么,然后相应地手动更改它。您可以控制路径列表的生成吗?如果您可以确保指向目录的所有路径都以尾随斜杠结尾(我相信这是惯例),那么简单的
Path.GetDirectoryName(Path)
将适用于所有这些情况。对于任何用户,我的代码都有一个潜在的错误。如果路径是指向服务器根目录中不存在的文件(或不带尾随斜杠的目录)的UNC样式路径(\\server\someblah
),您将返回null
,而不是预期的\\server
,因为\\server
不是Path.GetDirectoryName
使用的规则所使用的目录。是的,你是对的,不知道我怎么会错过它!不幸的是,我无法控制提供的路径(这是用户输入),但我认为您的答案与其他答案一样正确。