By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

Open file without knowing the extension

P: n/a
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

Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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!
Jun 27 '08 #2

P: n/a
On May 15, 5:00*pm, "Ron" <rs_herh...@yahoo.comwrote:
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
Jun 27 '08 #3

P: n/a
On Thu, 15 May 2008 07:13:29 -0700, S Chapman <s_*********@hotmail.co.uk>
wrote:
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!
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
Jun 27 '08 #4

P: n/a
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

"kimiraikkonen" <ki*************@gmail.comwrote in message
news:a8**********************************@24g2000h sh.googlegroups.com...
On May 15, 5:00 pm, "Ron" <rs_herh...@yahoo.comwrote:
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

Jun 27 '08 #5

P: n/a
Ron
Now that is what Im talking about!

Thanks Pete!

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 15 May 2008 07:13:29 -0700, S Chapman <s_*********@hotmail.co.uk>
wrote:
>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!

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
Jun 27 '08 #6

P: n/a


"Ron" <rs********@yahoo.comwrote in message
news:1B**********************************@microsof t.com...
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

Jun 27 '08 #7

P: n/a


"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 15 May 2008 07:13:29 -0700, S Chapman <s_*********@hotmail.co.uk>
wrote:
>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!

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
Jun 27 '08 #8

P: n/a
On Thu, 15 May 2008 08:25:28 -0700, Ron <rs********@yahoo.comwrote:
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 ""
The Path class (not "path") is used to manipulate strings that represent
paths. Whether those strings represent an actual file or not is
irrelevant to the Path class. It only cares about the string itself. In
that context, it makes perfect sense that a string without any extension
would return "" if there's no extension specified in the string.

Pete
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.