Remove file extension from a file name string

tweetypi picture tweetypi · Sep 9, 2011 · Viewed 249k times · Source

If I have a string saying "abc.txt", is there a quick way to get a substring that is just "abc"?

I can't do an fileName.IndexOf('.') because the file name could be "abc.123.txt" or something and I obviously just want to get rid of the extension (i.e. "abc.123").

Answer

R. Martinho Fernandes picture R. Martinho Fernandes · Sep 9, 2011

The Path.GetFileNameWithoutExtension method gives you the filename you pass as an argument without the extension, as should be obvious from the name.