tags:

views:

725

answers:

4

Hi,

I have folder "Icons". I need to access same inorder 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"]));

Can anyone pls guide me.

+4  A: 

Try adding the current running path:

smallImageList.Images.Add(Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationSettings.AppSettings["doc"])));
Scoregraphic
Thanks for that , that working ..even Path.GetFullPath(ConfigurationSettings.AppSettings["doc"]); this works..i wanted to know is im doin something wrong in smallImageList.Images.Add(Image.FromFile(ConfigurationSettings.AppSettings["doc"]));
Andy
smallImageList.Images.Add(Image.FromFile(Path.GetFullPath(ConfigurationSettings.AppSettings["doc"])));
Andy
A: 

Try using a tilda...

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

Which should start you from the application root.

Sohnee
+1  A: 

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

Eric Nicholson
Wow, I didn't know that one. much much simpler that my GetExecutingAssembly() solution...
Vinzz
A: 

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"])));
Vinzz
hi Vinzz,it retrieves the path C:\Documents and Settings\ADMIN\My Documents\Visual Studio 2008\Projects\IconsLoadinginTreeview_Files_Demo\IconsLoadinginTreeview_Files_Demo\bin\Debug\..\Icons\_Microsoft Office Excel 97-2003 Worksheet.ico ".." in the filPath again throwing error
Andy
Of course, your mileage may vary. Add an extra '..' there or there if necessary. I don't know where your icons really are ;o) Btw, it seems you already found a solution, isn't it?
Vinzz