Relative path app.config

I have an "Icons" folder. I need to access the same in order to add an icon to the imageList . I am using the app.config file which has a relative path.

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

and I use the code below to add it to imgList , however it throws a System.IO.FileNotFoundException :

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

What is the problem?

+6
c # app-config
source share
5 answers

Try adding the current current path:

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

You may need to combine this with System.AppDomain.CurrentDomain.BaseDirectory.

I would suggest that FromFile refers to the current working directory, which is subject to change. Another thing to consider is embedding images in an assembly.

+2
source share

Go to properties, find the Copy to Output Directory property, and select Copy Always. Then it should be good. Hope this helps.

+2
source share

Try using the tilde ...

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

What should start from the root of the application.

0
source share

Your working folder was somehow changed during the execution of your program, you need to find your own path.

Try the following:

 using System.Reflection; string CurrDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); smallImageList.Images.Add(Image.FromFile(Path.Combine(CurrDirectory,ConfigurationSettings.AppSettings["doc"]))); 
0
source share

All Articles