C# – Remove file extension from a file name string

cparsingstring

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").

Best Answer

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