1

I'm working on WPF C# ans I'm using Visual Studio 2017.

It is to set the file path of appSettings. I want to define a relative path in App.Config file. 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

jordanchase
  • 86
  • 1
  • 5

1 Answers1

4

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

You can retrieve the path associated to this enum using Environment.GetFolderPath passing the enum entry

 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

 string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
 string configLocation = Configuration.AppSettings["file"].ToString();
 string file = Path.Combine(appData, configLocation);
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thank you for your answer but it is not really what I want. My goal is to set a relative path in the file property of appSettings. Yous answser is to store a path in App.config. AppSettings allow me to use an external config file and what I need is to set the path of this external file. – jordanchase Feb 26 '19 at 08:46
  • Ah! Yes then I have misunderstood the question. But did you try to not use anything in front of your path? It should be relative to the current position of your assembly, Usually this is the BIN\DEBUG folder when debugging in VS and the exe storage location in deployed scenario – Steve Feb 26 '19 at 08:50
  • In other words. While running your app inside Visual Studio I would try to create a subfolder under BIN\DEBUG named _Test_ and put there the file _appfile.config_ and change the __ – Steve Feb 26 '19 at 08:56
  • Yes I tried and it works in bin/debug but I have to use an external folder. If I use the exe folder, I can not set a sepecific configuration for each user/computer. That's why I have to use an external folder as AppData. – jordanchase Feb 26 '19 at 08:57
  • 2
    It seems that this problem has been solved by the new _applicationSettings_ section with the user config option. This will create separate settings for each user. Here some discussion about these settings https://stackoverflow.com/questions/460935/pros-and-cons-of-appsettings-vs-applicationsettings-net-app-config-web-confi – Steve Feb 26 '19 at 09:00