Thursday, August 28, 2008

IsImageFile

There is no such method in FCL, so here is my implementation :) Yes, it is not good to use exceptions this way, but it's most elegant decision from the range that I considered.
        public static bool IsImageFile(string fileName)
{
try
{
using (Image img = Image.FromFile(fileName))
{
return true;
}
}
catch (OutOfMemoryException)
{
return false;
}
}

4 comments:

plotnick said...

huh, it's so slily point :)

Bashir Magomedov said...

Thank you, but I'm really not sure that there are no any other solutions that comply better with OOP concepts. :)

Nesusvet said...

Какой ужасный, избыточный язык. Другое дело python:

def IsImageFile(fileName):
try:
using (Image img = Image.FromFile(fileName))
return true
except OutOfMemoryException:
return false

Nesusvet said...

Блин, в комментах форматирование не работает. =(