c# 4.0 -从c#的完整路径返回不带扩展名的文件名

本文关键字:返回 文件名 扩展名 路径 | 更新日期: 2023-09-27 17:49:37

我正在寻找一种从完整路径返回fileName但没有扩展的方法。

    private static string ReturnFileNameWithoutExtension(string varFullPathToFile) {
        string fileName = new FileInfo(varFullPathToFile).Name;
        string extension = new FileInfo(varFullPathToFile).Extension;
        return fileName.Replace(extension, "");   
    }

是否有更多的防弹解决方案,然后用空字符串替换扩展?

c# 4.0 -从c#的完整路径返回不带扩展名的文件名

return Path.GetFileNameWithoutExtension (fullpath);

我使用System.IO.Path.GetFileNameWithoutExtension(filename);

您正在寻找Path。GetFileNameWithoutExtension

一个解决方案

string fileName = new FileInfo(varFullPathToFile).Name;
fileName=fileName.Substring(0, fileName.LastIndexOf("."));