Open file without knowing the extension

R

Ron

I am building a dynamic image loading class. I have a list of properties
that have associated images in a specified directory. The problem they are
multiple formats which are not known at runtime...and to make matters worse
users are allowed to use images in any format...so long as they have the
filename that matches the property name.

My problem stems from the fact that I dont know the extension of the
file...only the filename...so file.exists and new Bitmap(filename) dont work
without an extension. Is there anyway to dynamically retrieve the extension
from a file...or open it without using an extension?

Thanks,
Ron
 
S

S Chapman

Enumerate all the files in the folder and see if the primary name
matches the name you are after, if so you have found the file!
 
K

kimiraikkonen

I am building a dynamic image loading class.  I have a list of properties
that have associated images in a specified directory. The problem they are
multiple formats which are not known at runtime...and to make matters worse
users are allowed to use images in any format...so long as they have the
filename that matches the property name.

My problem stems from the fact that I dont know the extension of the
file...only the filename...so file.exists and new Bitmap(filename) dont work
without an extension.  Is there anyway to dynamically retrieve the extension
from a file...or open it without using an extension?

Thanks,
Ron

Ron,
You can get a file's extension at runtime using
"System.io.path.GetExtension" method. Then you can write your own "if-
else if" structures to determine what to do with each image files
having specified extension.

Thanks,

Onur Güzel
 
R

Ron

The only problem with that is that the path.GetExtension method expects an
extension to be specified (which seems a little counter intuitive if you ask
me), else it returns ""

Thanks though.

Ron

I am building a dynamic image loading class. I have a list of properties
that have associated images in a specified directory. The problem they are
multiple formats which are not known at runtime...and to make matters
worse
users are allowed to use images in any format...so long as they have the
filename that matches the property name.

My problem stems from the fact that I dont know the extension of the
file...only the filename...so file.exists and new Bitmap(filename) dont
work
without an extension. Is there anyway to dynamically retrieve the
extension
from a file...or open it without using an extension?

Thanks,
Ron

Ron,
You can get a file's extension at runtime using
"System.io.path.GetExtension" method. Then you can write your own "if-
else if" structures to determine what to do with each image files
having specified extension.

Thanks,

Onur Güzel
 
R

Ron

Now that is what Im talking about!

Thanks Pete!

Peter Duniho said:
And specifically, see the Directory.GetFiles(string, string) method, which
allows you to pass a search string. If you have the name of the directory
as "strDirectory" and the name of the property as "strProperty", then
calling Directory.GetFiles(strDirectory, strProperty + "*") will retrieve
the actual filenames of every file in the directory that starts with the
property name.

You can then enumerate those files (if any), attempting to open each one
with Image.FromFile(), using the first one that succeeds.

Pete
 
M

Mythran

Ron said:
I am building a dynamic image loading class. I have a list of properties
that have associated images in a specified directory. The problem they are
multiple formats which are not known at runtime...and to make matters
worse users are allowed to use images in any format...so long as they have
the filename that matches the property name.

My problem stems from the fact that I dont know the extension of the
file...only the filename...so file.exists and new Bitmap(filename) dont
work without an extension. Is there anyway to dynamically retrieve the
extension from a file...or open it without using an extension?

Thanks,
Ron

It's actually pretty easy to do this. You don't need to enumerate through
all of the files in the folder either. All you need is to use the GetFiles
method of System.IO.Directory.

IE:

string[] files = Directory.GetFiles(@"C:\images", @"myImage.*");

foreach (string file in files) {
Console.WriteLine("File " + file + " matches the search pattern.");
}


NOTE:
There is probably another way to do this using a more recent version of the
..Net Framework, but our shop here has just recently purchased Visual Studio
..Net 2008 and have not yet installed it. We are still working with VIsual
Studio .Net 2003 and the v1.1 framework. :p

HTH,
Mythran
 
M

Mythran

Peter Duniho said:
And specifically, see the Directory.GetFiles(string, string) method, which
allows you to pass a search string. If you have the name of the directory
as "strDirectory" and the name of the property as "strProperty", then
calling Directory.GetFiles(strDirectory, strProperty + "*") will retrieve
the actual filenames of every file in the directory that starts with the
property name.

You can then enumerate those files (if any), attempting to open each one
with Image.FromFile(), using the first one that succeeds.

Pete

Whoops, must have been replying when you posted this....so yeah, ignore my
post since this one says the same :)

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top