简体   繁体   English

如何在App.Config中设置相对路径?

[英]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.

相关问题 App.config相对路径 - App.config relative path 在Windows 8上无法访问App.Config的路径。如何更新App.Config? - Access to path denied for App.Config on Windows 8. How can I update App.Config? 如何在app.config文件中设置相对路径? - How to set relative paths in app.config file? 如何为要在App.Config文件中读取的文件设置通用路径 - How to set a universal path for the files to be read in an App.Config file 我可以在web.config或app.config中设置条件编译常量吗? - Can I set a conditional compilation constant in a web.config or app.config, if so how? Winforms应用程序的app.config的名称是什么? 以及我怎么知道该应用程序已加载该app.config? - what is the name of the app.config of a winforms app? and how can i know that the app loaded that app.config? 我可以在App.config中为maxJsonLength设置无限长度吗? - Can I set an unlimited length for maxJsonLength in App.config? 在app.config中创建连接字符串attachdbfilename的相对路径 - Create relative path to connection string attachdbfilename in app.config App.config中的相对路径指向另一个项目 - Relative path in App.config is pointing at a different project 如何在app.config中动态设置WCF服务基地址端口号? - How can I set a WCF service base address port number dynamically in app.config?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM