# Wednesday, August 26, 2009
How to get the file extension from string file name?

Well brute force approach would be something like that:

string fileExtension = args.FileName.Split('.').GetValue(args.FileName.Split('.').Length - 1).ToString();

Pretty ugly, in spite of one liner code.

Well it turns out that the framework has support for this functionality:

string fileExtension = System.IO.Path.GetExtension(fileName);

Much better, right?

It is good to remember that .NET Framework contain a lot of functionality that you would not think about at first place. That's why is good to check every time is something smells that it could be in .NET Framework. You may find it.

posted on Wednesday, August 26, 2009 4:43:41 PM (Central European Daylight Time, UTC+02:00)  #    Comments [0] Trackback
Related posts: