在C#中从完整路径返回没有扩展名的文件名

时间:2011-04-11 17:02:57

标签: c# c#-4.0

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

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

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

4 个答案:

答案 0 :(得分:30)

return Path.GetFileNameWithoutExtension (fullpath);

答案 1 :(得分:6)

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

答案 2 :(得分:3)

答案 3 :(得分:0)

再解决方案

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