2011-04-11 110 views
7

我正在尋找一種方法來從完整路徑,但沒有擴展名返回fileName。返回文件名沒有擴展名從C#中的完整路徑#

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

是否有更多的防彈解決方案,然後用空字符串替換擴展名?

回答

29
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

6

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

0

再來解決

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