473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Open file without knowing the extension

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
8 7442
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
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
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
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
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


"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


"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bethany Holliday | last post by:
Hi all, I have a file with an extension of .sdf. I "believe" it is a text file of some sort but I am uncertain. The source agency hasn't returned any of my calls so I'm wondering if anyone is...
2
by: RSB | last post by:
Hi Every one, I am currently using Visual Studio .Net and trying to open a project from SourceSafe and once i open the project for the first time i get the following error/warning... "The...
29
by: wayne | last post by:
Hey there... I'm having some problems passing url parameters with an open.window command. I'm not terribly familiar with java script but here is the code below. When executed it opens the...
6
by: Kenneth Courville | last post by:
Hello, I'm looking for assistance with the Access object model. I know this is VB, but I'm building an Office Add-using C# directed at Access 2002. I'm literate in VB, so you can reply in VB... I...
2
by: collinm | last post by:
i try to search txt file in a folder my code int searchfile(char *dir, char *filename, int flength, char *ext) { DIR *pdir; pdir = opendir(dir); struct dirent *pent; if (!pdir)
9
by: ferbar | last post by:
Hi all, I'm trying to read from the txt file 'ip.packets.2.txt' using the read function. It seems everything ok, but I get a -1 when executing >>bytesr = read(fdo1, bufread, 2); The 'open'...
6
by: Frieder Bürzele | last post by:
Hi, Im trying to integrate a rss feed into my page but struggle to fetch the dynamic generation of the rss feed If I type this into the browser as a result the xml file is shown as expected...
5
by: Chuck Anderson | last post by:
I run Apache 2.0.55, and Php (both 4.4.1 and 5.2.5) on my home PC (Windows XP). One of the scripts that I run daily needs to access a secure URL (https://..............). When I am running Php4,...
7
by: Service4PC | last post by:
Hi all, I've a problem... I'm writing an extension for php, and i need to execute a file php... but only the content of the file and not the file... Now I better explain: - the extension get the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.