Anees
Anees

Reputation: 873

App.config relative path

I have folder "Icons". I need to access same in order to add an icon to imageList. I'm using app.config file in that have a relative path.

<add key="doc" value="..\Icons\_Microsoft Office Excel 97-2003 Worksheet.ico" />

and I'm using below code to add it to imgList ,however it throws System.IO.FileNotFoundException:

smallImageList.Images.Add(Image.FromFile(ConfigurationSettings.AppSettings["doc"]));

What's the problem here?

Upvotes: 6

Views: 15827

Answers (5)

curiousBoy
curiousBoy

Reputation: 6834

Go to properties , find 'Copy to Output Directory' property and choose "Copy always" . Then it should be fine. Hope it will help.

Upvotes: 2

Vinzz
Vinzz

Reputation: 4028

Your working folder has somehow been modified during your program execution, you have to find your own path.

Try this:

using System.Reflection;
string CurrDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

smallImageList.Images.Add(Image.FromFile(Path.Combine(CurrDirectory,ConfigurationSettings.AppSettings["doc"])));

Upvotes: 0

Eric Nicholson
Eric Nicholson

Reputation: 4123

You might need to concatenate that with System.AppDomain.CurrentDomain.BaseDirectory.

I'd guess that FromFile is relative to the current working directory which is prone to change. The other thing to consider would be embedding the images in the assembly

Upvotes: 2

Fenton
Fenton

Reputation: 251192

Try using a tilda...

value="~\Icons_Microsoft Office Excel 97-2003 Worksheet.ico"

Which should start you from the application root.

Upvotes: 0

Scoregraphic
Scoregraphic

Reputation: 7200

Try adding the current running path:

smallImageList.Images.Add(Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationSettings.AppSettings["doc"])));

Upvotes: 8

Related Questions