C# 如何在 .config 文件中指定相对于文件的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16168154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify path in .config file relative to the file?
提问by Colonel Panic
An app I use interprets a .NET .configfile. I added a line specifying the path to a certificate it needs
我使用的一个应用程序解释了一个 .NET.config文件。我添加了一行指定它需要的证书的路径
<add key="Certificate" value="..\certificate.abc"/>
However, I found the app only works when run from the exact folder its .exeand .configreside. If run from another folder, it fails, because it looks for the certificate at ../expanded relative to the working directory.
然而,我发现从确切的文件夹时运行的应用程序只能.exe和.config所在的位置。如果从另一个文件夹运行,它会失败,因为它会在相对于工作目录的../扩展位置查找证书。
What should I write in the config file, to make sure the path is ..\certificate.abcexpanded relative to the config filerather the working directory?
我应该在配置文件中写什么,以确保路径..\certificate.abc扩展相对于配置文件,而工作目录?
I can't change the app (it's not mine), I can only change the config file.
我无法更改应用程序(它不是我的),我只能更改配置文件。
回答by SpaceApple
By using the tilde
通过使用波浪号
value="~/certificate.abc"
回答by Anton Baksheiev
I think in your case you need set file name. Folder where all files you know (if folder can be canged - set folder without ../)
我认为在您的情况下,您需要设置文件名。您知道的所有文件所在的文件夹(如果文件夹可以被隐藏 - 设置没有 ../ 的文件夹)
And where you need use path - combine current location of application(can be got in runtime ) and key from settings
以及您需要使用路径的地方 - 结合应用程序的当前位置(可以在运行时获得)和设置中的密钥
or use
或使用
System.Web.HttpContext.Current.Server.MapPath("~/[PARTH_TO_FILE]") '
回答by Jim Mischel
You can't change this just by changing the config file. Unless you use an absolute path.
您不能仅通过更改配置文件来更改此设置。除非你使用绝对路径。
If you can change the application ...
如果您可以更改应用程序...
If this is an asp.net application, then use Server.MapPath. If it's a Windows app, you need to get the directory of the executing application and combine that with the relative path in your config file.
如果这是一个 asp.net 应用程序,则使用Server.MapPath. 如果它是 Windows 应用程序,则需要获取正在执行的应用程序的目录,并将其与配置文件中的相对路径结合起来。
System.Reflection.Assembly.GetExecutingAssembly().Locationwill give you the path to the .EXE file. You can then call Path.Combineto combine your relative path with the application's path.
System.Reflection.Assembly.GetExecutingAssembly().Location将为您提供 .EXE 文件的路径。然后您可以调用Path.Combine将您的相对路径与应用程序的路径结合起来。
回答by JSJ
you config file may lie on other location then the executing file. as you mantion executing path i understand this is the desktop based application so here you go what you can do like this.
您的配置文件可能位于其他位置,然后是执行文件。当你提到执行路径时,我明白这是基于桌面的应用程序,所以你可以这样做。
in Config.
在配置中。
<setting name="FilePath" serializeAs="String">
<value>AppPath\MyFile.abc</value>
</setting>
and to retrive this.
并检索这个。
var path = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
if (path !=null && path.Contains("AppPath"))
{
var filepath = System.IO.Path.Combine(
System.Reflection.Assembly.GetExecutingAssembly().Location,
path.Replace("AppPath", string.Empty).ToString());
Console.WriteLine(filepath);
}
回答by Hai Hoang
In visual studio 2015, the "~" keyword has no longer worked. You can now use :
在 Visual Studio 2015 中,“~”关键字不再起作用。您现在可以使用:
value="folder\subfoler"
"folder" has the same level with .exe file
“文件夹”与 .exe 文件级别相同
回答by aolisa
..\goes up a folder level. Repeat as many times as needed e.g. ..\..\to get to your bin folder.
..\上升一个文件夹级别。根据需要重复多次,例如..\..\进入您的 bin 文件夹。

