Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.config relative path

Tags:

c#

app-config

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?

like image 432
Anees Avatar asked Aug 26 '09 12:08

Anees


2 Answers

Try adding the current running path:

smallImageList.Images.Add(Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationSettings.AppSettings["doc"])));
like image 50
Scoregraphic Avatar answered Sep 28 '22 19:09

Scoregraphic


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

like image 42
Eric Nicholson Avatar answered Sep 28 '22 17:09

Eric Nicholson