Friday, February 01, 2008

Strip File Extension from Filename in .Net

Today I need to get a filename without the extension in .Net. It turn out that it's pretty easy. It's not so obvious from looking at MSDN (going through references for string and FileInfo give unsatisfying result). Being lazy to code this kind of stuff myself if I don't really have to, some googling get me more enlightening discussion.

Surprisingly, the line below return what I need :

Path.GetFileNameWithoutExtension(fileNameStr);

It's pretty straightforward and very convenient. It makes me wonder what people will make next, probably something like :

fileNameStr.AssumeThisStringIsFilenameAndGetItsNameWithoutTheExtension();

that would makes my work easier, although it probably would annoy someone else a little bit :).

No comments: