Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# App.config相对路径_C#_App Config - Fatal编程技术网

C# App.config相对路径

C# App.config相对路径,c#,app-config,C#,App Config,我有文件夹“图标”。 我需要访问相同的图标才能将图标添加到imageList。 我正在使用具有相对路径的app.config文件 <add key="doc" value="..\Icons\_Microsoft Office Excel 97-2003 Worksheet.ico" /> 这里有什么问题?尝试添加当前运行路径: smallImageList.Images.Add(Image.FromFile(Path.Combine(AppDomain.CurrentDomain

我有文件夹“图标”。 我需要访问相同的图标才能将图标添加到
imageList
。 我正在使用具有相对路径的app.config文件

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

这里有什么问题?

尝试添加当前运行路径:

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

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

从应用程序根目录开始。您可能需要将其与System.AppDomain.CurrentDomain.BaseDirectory连接起来


我猜FromFile是相对于当前工作目录的,它很容易改变。要考虑的另一件事是将图像嵌入到程序集

你的工作文件夹在程序执行过程中已经被修改了,你必须找到你自己的路径。 试试这个:

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

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

转到属性,找到“复制到输出目录”属性并选择“始终复制”。那就好了。希望这会有帮助

感谢您的支持,它可以工作..甚至Path.GetFullPath(ConfigurationSettings.AppSettings[“doc”]);我想知道我在smallImageList.Images.Add(Image.FromFile(ConfigurationSettings.AppSettings[“doc]”)中是否出错;smallImageList.Images.Add(Image.FromFile(Path.GetFullPath(ConfigurationSettings.AppSettings[“doc”]));这应该被标记为答案。问题似乎是
Image.FromFile
要求的是绝对路径,而不是相对路径。哇,我不知道那个路径。比我的GetExecutionGassembly()解决方案简单得多…嗨,Vinzz,它检索路径C:\Documents and Settings\ADMIN\my Documents\Visual Studio 2008\Projects\IConsLoadingTreeview\u Files\u Demo\IConsLoadingTreeview\u Files\u Demo\bin\Debug\…\Icons\u Microsoft Office Excel 97-2003 Worksheet.ico“当然,在filPath中,您的里程可能会有所不同。如有必要,在此处或此处添加额外的“..”。我不知道你的图标到底在哪里;o) 顺便说一句,看来你已经找到了解决办法,不是吗?
using System.Reflection;
string CurrDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

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