简体   繁体   English

App.config相对路径

[英]App.config relative path

I have folder "Icons". 我有文件夹“图标”。 I need to access same in order to add an icon to imageList . 我需要访问相同才能向imageList添加图标。 I'm using app.config file in that have a relative path. 我正在使用具有相对路径的app.config文件。

<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 : 我正在使用下面的代码将其添加到imgList ,但它会抛出System.IO.FileNotFoundException

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

What's the problem here? 这有什么问题?

尝试添加当前运行路径:

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

You might need to concatenate that with System.AppDomain.CurrentDomain.BaseDirectory. 您可能需要将其与System.AppDomain.CurrentDomain.BaseDirectory连接。

I'd guess that FromFile is relative to the current working directory which is prone to change. 我猜想FromFile是相对于当前工作目录很容易改变的。 The other thing to consider would be embedding the images in the assembly 另一件需要考虑的事情是将图像嵌入到程序集中

Go to properties , find 'Copy to Output Directory' property and choose "Copy always" . 转到属性,找到“复制到输出目录”属性,然后选择“始终复制”。 Then it should be fine. 那应该没问题。 Hope it will help. 希望它会有所帮助。

Try using a tilda... 尝试使用蒂尔达...

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

Which should start you from the application root. 哪个应该从应用程序根目录开始。

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"])));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM