C#获取文件名没有扩展 [英] C# getting file names without extensions

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

问题描述

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

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.Name 我们得到了其扩展名的文件名: FILE1.TXT FILE2.TXT file3.txt

by fi.Name we get a file name with its extension: file1.txt, file2.txt, file3.txt

如何更好地获取文件名没有文件扩展名文件1 文件2 文件3

How better to get file names without file extensions file1, file2, file3?

推荐答案

您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx\"><$c$c>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).

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

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