[英]How can I set a relative path in App.Config?
I'm working on WPF C# ans I'm using Visual Studio 2017. 我正在使用WPF C#和Visual Studio 2017。
It is to set the file path of appSettings. 用来设置appSettings的文件路径。 I want to define a relative path in App.Config file. 我想在App.Config文件中定义一个相对路径。 If I try with absolute path it works. 如果我尝试使用绝对路径,它会起作用。
This work: 这项工作:
<appSettings file="C:\Users\jordan\AppData\Roaming\Test\appfile.config">
But if I try with relative path it does not work. 但是,如果我尝试使用相对路径,它将无法正常工作。
This does not work: 这不起作用:
<appSettings file="${AppData}\Roaming\Test\appfile.config">
<appSettings file="~\AppData\Roaming\Test\appfile.config">
<appSettings file="~/AppData/Roaming/Test/appfile.config">
etc...
Do you have an idea to set a relative path here? 您是否有想法在此处设置相对路径? Or just why I can't do that? 还是为什么我不能做到这一点?
Thanks 谢谢
The application specific folder for the current roaming user is defined by one entry in the enum Environment.SpecialFolder and it is the ApplicationData entry 当前漫游用户的特定于应用程序的文件夹由枚举Environment.SpecialFolder中的一个条目定义,它是ApplicationData条目
You can retrieve the path associated to this enum using Environment.GetFolderPath passing the enum entry 您可以使用Environment.GetFolderPath传递枚举条目来检索与此枚举关联的路径。
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
So in your config file you could store simply the final part Test\\appfile.config and use this code 因此,在您的配置文件中,您可以仅存储最后一部分Test \\ appfile.config并使用此代码
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string configLocation = Configuration.AppSettings["file"].ToString();
string file = Path.Combine(appData, configLocation);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.