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

问题描述:

我正在寻找一种方法来从完整路径,但没有扩展名返回fileName。返回文件名没有扩展名从C#中的完整路径#

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

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

return Path.GetFileNameWithoutExtension (fullpath); 
+0

你打我吧:P – 2011-04-11 17:05:07

+0

感谢。错过了这个:) – MadBoy 2011-04-11 17:29:55

+0

如果你还没有完整的路径,该怎么办? – Boomerang 2014-11-12 16:29:20

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

再来解决

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