首先,问题中的代码不会产生所描述的输出。它提取文件 extension ("txt") 而不是文件 base name ("hello")。要做到这一点,最后一行应该调用First(),而不是Last(),像这样......
static string GetFileBaseNameUsingSplit(string path)
{
string[] pathArr = path.Split('\\');
string[] fileArr = pathArr.Last().Split('.');
string fileBaseName = fileArr.First().ToString();
return fileBaseName;
}
进行了更改后,就改进此代码而言,需要考虑的一件事是它产生的垃圾量:
- 一个
string[] 包含一个string 用于path 中的每个路径段
- 一个
string[],在path 的最后一个路径段中,每个. 至少包含一个string
因此,从示例路径"C:\Program Files\hello.txt" 中提取基本文件名应生成(临时)objects "C:"、"Program Files"、"hello.txt"、"hello"、"txt"、@987654343 @ 和 string[2]。如果在大量路径上调用该方法,这可能很重要。为了改进这一点,我们可以自己搜索path 来定位基本名称的起点和终点,并使用它们来创建一个新的string...
static string GetFileBaseNameUsingSubstringUnsafe(string path)
{
// Fails on paths with no file extension - DO NOT USE!!
int startIndex = path.LastIndexOf('\\') + 1;
int endIndex = path.IndexOf('.', startIndex);
string fileBaseName = path.Substring(startIndex, endIndex - startIndex);
return fileBaseName;
}
这是使用最后一个 \ 之后的字符索引作为基本名称的开头,并从那里寻找第一个 . 以用作基本名称结尾之后的字符索引.这比原始代码短吗?不完全的。这是一个“更智能”的解决方案吗?我想是这样。至少,如果不是因为……
从评论中可以看出,前面的方法是有问题的。虽然假设所有路径都以带有扩展名的文件名结尾,但如果路径以\(即目录路径)结尾或最后一段中不包含扩展名,它将引发异常。为了解决这个问题,我们需要添加一个额外的检查来说明endIndex 何时为-1(即. 未找到)...
static string GetFileBaseNameUsingSubstring(string path)
{
int startIndex = path.LastIndexOf('\\') + 1;
int endIndex = path.IndexOf('.', startIndex);
int length = (endIndex >= 0 ? endIndex : path.Length) - startIndex;
string fileBaseName = path.Substring(startIndex, length);
return fileBaseName;
}
现在这个版本比原来的版本短了很多,但它更有效并且(现在)也正确。
就实现此功能的 .NET 方法而言,许多其他答案建议使用 Path.GetFileNameWithoutExtension(),这是一个显而易见且简单的解决方案,但 不会产生与问题中的代码相同的结果 . GetFileBaseNameUsingSplit() 和 Path.GetFileNameWithoutExtension() 之间有一个微妙但重要的区别(GetFileBaseNameUsingPath() 下面):前者提取 first . 之前的所有内容,而后者提取 last 之前的所有内容.。这对问题中的示例path 没有什么影响,但是看看这张表比较了上述四种方法在不同路径下调用时的结果...
| Description |
Method |
Path |
Result |
| Single extension |
GetFileBaseNameUsingSplit() |
"C:\Program Files\hello.txt" |
"hello" |
| Single extension |
GetFileBaseNameUsingPath() |
"C:\Program Files\hello.txt" |
"hello" |
| Single extension |
GetFileBaseNameUsingSubstringUnsafe() |
"C:\Program Files\hello.txt" |
"hello" |
| Single extension |
GetFileBaseNameUsingSubstring() |
"C:\Program Files\hello.txt" |
"hello" |
|
|
|
|
| Double extension |
GetFileBaseNameUsingSplit() |
"C:\Program Files\hello.txt.ext" |
"hello" |
| Double extension |
GetFileBaseNameUsingPath() |
"C:\Program Files\hello.txt.ext" |
"hello.txt" |
| Double extension |
GetFileBaseNameUsingSubstringUnsafe() |
"C:\Program Files\hello.txt.ext" |
"hello" |
| Double extension |
GetFileBaseNameUsingSubstring() |
"C:\Program Files\hello.txt.ext" |
"hello" |
|
|
|
|
| No extension |
GetFileBaseNameUsingSplit() |
"C:\Program Files\hello" |
"hello" |
| No extension |
GetFileBaseNameUsingPath() |
"C:\Program Files\hello" |
"hello" |
| No extension |
GetFileBaseNameUsingSubstringUnsafe() |
"C:\Program Files\hello" |
EXCEPTION: Length cannot be less than zero. (Parameter 'length') |
| No extension |
GetFileBaseNameUsingSubstring() |
"C:\Program Files\hello" |
"hello" |
|
|
|
|
| Leading period |
GetFileBaseNameUsingSplit() |
"C:\Program Files\.hello.txt" |
"" |
| Leading period |
GetFileBaseNameUsingPath() |
"C:\Program Files\.hello.txt" |
".hello" |
| Leading period |
GetFileBaseNameUsingSubstringUnsafe() |
"C:\Program Files\.hello.txt" |
"" |
| Leading period |
GetFileBaseNameUsingSubstring() |
"C:\Program Files\.hello.txt" |
"" |
|
|
|
|
| Trailing period |
GetFileBaseNameUsingSplit() |
"C:\Program Files\hello.txt." |
"hello" |
| Trailing period |
GetFileBaseNameUsingPath() |
"C:\Program Files\hello.txt." |
"hello.txt" |
| Trailing period |
GetFileBaseNameUsingSubstringUnsafe() |
"C:\Program Files\hello.txt." |
"hello" |
| Trailing period |
GetFileBaseNameUsingSubstring() |
"C:\Program Files\hello.txt." |
"hello" |
|
|
|
|
| Directory path |
GetFileBaseNameUsingSplit() |
"C:\Program Files\" |
"" |
| Directory path |
GetFileBaseNameUsingPath() |
"C:\Program Files\" |
"" |
| Directory path |
GetFileBaseNameUsingSubstringUnsafe() |
"C:\Program Files\" |
EXCEPTION: Length cannot be less than zero. (Parameter 'length') |
| Directory path |
GetFileBaseNameUsingSubstring() |
"C:\Program Files\" |
"" |
|
|
|
|
| Current file path |
GetFileBaseNameUsingSplit() |
"hello.txt" |
"hello" |
| Current file path |
GetFileBaseNameUsingPath() |
"hello.txt" |
"hello" |
| Current file path |
GetFileBaseNameUsingSubstringUnsafe() |
"hello.txt" |
"hello" |
| Current file path |
GetFileBaseNameUsingSubstring() |
"hello.txt" |
"hello" |
|
|
|
|
| Parent file path |
GetFileBaseNameUsingSplit() |
"..\hello.txt" |
"hello" |
| Parent file path |
GetFileBaseNameUsingPath() |
"..\hello.txt" |
"hello" |
| Parent file path |
GetFileBaseNameUsingSubstringUnsafe() |
"..\hello.txt" |
"hello" |
| Parent file path |
GetFileBaseNameUsingSubstring() |
"..\hello.txt" |
"hello" |
|
|
|
|
| Parent directory path |
GetFileBaseNameUsingSplit() |
".." |
"" |
| Parent directory path |
GetFileBaseNameUsingPath() |
".." |
"." |
| Parent directory path |
GetFileBaseNameUsingSubstringUnsafe() |
".." |
"" |
| Parent directory path |
GetFileBaseNameUsingSubstring() |
".." |
"" |
...您会看到Path.GetFileNameWithoutExtension() 在传递文件名具有双扩展名或前导和/或尾随. 的路径时会产生不同的结果。你可以用下面的代码自己试试……
using System;
using System.IO;
using System.Linq;
using System.Reflection;
namespace SO6921105
{
internal class PathExtractionResult
{
public string Description { get; set; }
public string Method { get; set; }
public string Path { get; set; }
public string Result { get; set; }
}
public static class Program
{
private static string GetFileBaseNameUsingSplit(string path)
{
string[] pathArr = path.Split('\\');
string[] fileArr = pathArr.Last().Split('.');
string fileBaseName = fileArr.First().ToString();
return fileBaseName;
}
private static string GetFileBaseNameUsingPath(string path)
{
return Path.GetFileNameWithoutExtension(path);
}
private static string GetFileBaseNameUsingSubstringUnsafe(string path)
{
// Fails on paths with no file extension - DO NOT USE!!
int startIndex = path.LastIndexOf('\\') + 1;
int endIndex = path.IndexOf('.', startIndex);
string fileBaseName = path.Substring(startIndex, endIndex - startIndex);
return fileBaseName;
}
private static string GetFileBaseNameUsingSubstring(string path)
{
int startIndex = path.LastIndexOf('\\') + 1;
int endIndex = path.IndexOf('.', startIndex);
int length = (endIndex >= 0 ? endIndex : path.Length) - startIndex;
string fileBaseName = path.Substring(startIndex, length);
return fileBaseName;
}
public static void Main()
{
MethodInfo[] testMethods = typeof(Program).GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
.Where(method => method.Name.StartsWith("GetFileBaseName"))
.ToArray();
var inputs = new[] {
new { Description = "Single extension", Path = @"C:\Program Files\hello.txt" },
new { Description = "Double extension", Path = @"C:\Program Files\hello.txt.ext" },
new { Description = "No extension", Path = @"C:\Program Files\hello" },
new { Description = "Leading period", Path = @"C:\Program Files\.hello.txt" },
new { Description = "Trailing period", Path = @"C:\Program Files\hello.txt." },
new { Description = "Directory path", Path = @"C:\Program Files\" },
new { Description = "Current file path", Path = "hello.txt" },
new { Description = "Parent file path", Path = @"..\hello.txt" },
new { Description = "Parent directory path", Path = ".." }
};
PathExtractionResult[] results = inputs
.SelectMany(
input => testMethods.Select(
method => {
string result;
try
{
string returnValue = (string) method.Invoke(null, new object[] { input.Path });
result = $"\"{returnValue}\"";
}
catch (Exception ex)
{
if (ex is TargetInvocationException)
ex = ex.InnerException;
result = $"EXCEPTION: {ex.Message}";
}
return new PathExtractionResult() {
Description = input.Description,
Method = $"{method.Name}()",
Path = $"\"{input.Path}\"",
Result = result
};
}
)
).ToArray();
const int ColumnPadding = 2;
ResultWriter writer = new ResultWriter(Console.Out) {
DescriptionColumnWidth = results.Max(output => output.Description.Length) + ColumnPadding,
MethodColumnWidth = results.Max(output => output.Method.Length) + ColumnPadding,
PathColumnWidth = results.Max(output => output.Path.Length) + ColumnPadding,
ResultColumnWidth = results.Max(output => output.Result.Length) + ColumnPadding,
ItemLeftPadding = " ",
ItemRightPadding = " "
};
PathExtractionResult header = new PathExtractionResult() {
Description = nameof(PathExtractionResult.Description),
Method = nameof(PathExtractionResult.Method),
Path = nameof(PathExtractionResult.Path),
Result = nameof(PathExtractionResult.Result)
};
writer.WriteResult(header);
writer.WriteDivider();
foreach (IGrouping<string, PathExtractionResult> resultGroup in results.GroupBy(result => result.Description))
{
foreach (PathExtractionResult result in resultGroup)
writer.WriteResult(result);
writer.WriteDivider();
}
}
}
internal class ResultWriter
{
private const char DividerChar = '-';
private const char SeparatorChar = '|';
private TextWriter Writer { get; }
public ResultWriter(TextWriter writer)
{
Writer = writer ?? throw new ArgumentNullException(nameof(writer));
}
public int DescriptionColumnWidth { get; set; }
public int MethodColumnWidth { get; set; }
public int PathColumnWidth { get; set; }
public int ResultColumnWidth { get; set; }
public string ItemLeftPadding { get; set; }
public string ItemRightPadding { get; set; }
public void WriteResult(PathExtractionResult result)
{
WriteLine(
$"{ItemLeftPadding}{result.Description}{ItemRightPadding}",
$"{ItemLeftPadding}{result.Method}{ItemRightPadding}",
$"{ItemLeftPadding}{result.Path}{ItemRightPadding}",
$"{ItemLeftPadding}{result.Result}{ItemRightPadding}"
);
}
public void WriteDivider()
{
WriteLine(
new string(DividerChar, DescriptionColumnWidth),
new string(DividerChar, MethodColumnWidth),
new string(DividerChar, PathColumnWidth),
new string(DividerChar, ResultColumnWidth)
);
}
private void WriteLine(string description, string method, string path, string result)
{
Writer.Write(SeparatorChar);
Writer.Write(description.PadRight(DescriptionColumnWidth));
Writer.Write(SeparatorChar);
Writer.Write(method.PadRight(MethodColumnWidth));
Writer.Write(SeparatorChar);
Writer.Write(path.PadRight(PathColumnWidth));
Writer.Write(SeparatorChar);
Writer.Write(result.PadRight(ResultColumnWidth));
Writer.WriteLine(SeparatorChar);
}
}
}
TL;DR 问题中的代码在某些极端情况下的表现并不像许多人预期的那样。如果您要编写自己的路径操作代码,请务必考虑...
- ...如何定义“不带扩展名的文件名”(是第一个
. 之前的所有内容还是最后一个 . 之前的所有内容?)
- ...具有多个扩展名的文件
- ...没有扩展名的文件
- ...以
. 开头的文件
- ...带有结尾
. 的文件(在 Windows 上可能不会遇到,但它们是 possible)
- ...带有“扩展名”或以其他方式包含
. 的目录
- ...以
\ 结尾的路径
- ...相对路径
并非所有文件路径都遵循X:\Directory\File.ext的常用公式!