获取不带扩展名的文件名 [英] Getting file names without extensions

查看:158
本文介绍了获取不带扩展名的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某个文件夹中获取文件名时:

When getting file names in a certain folder:

DirectoryInfo di = new DirectoryInfo(currentDirName);
FileInfo[] smFiles = di.GetFiles("*.txt");
foreach (FileInfo fi in smFiles)
{
    builder.Append(fi.Name);
    builder.Append(", ");
    ...
}

fi 给我一个扩展名为的文件名: file1.txt file2.txt file3.txt

fi.Name gives me a file name with its extension: file1.txt, file2.txt, file3.txt.

如何获取不带扩展名的文件名? ( file1 file2 file3

How can I get the file names without the extensions? (file1, file2, file3)

推荐答案

您可以使用 Path.GetFileNameWithoutExtension

You can use Path.GetFileNameWithoutExtension:

foreach (FileInfo fi in smFiles)
{
    builder.Append(Path.GetFileNameWithoutExtension(fi.Name));
    builder.Append(", ");
}

尽管我很惊讶,没有办法直接从 FileInfo (或者至少我看不到它)。

Although I am surprised there isn't a way to get this directly from the FileInfo (or at least I can't see it).

这篇关于获取不带扩展名的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆