3

How do I point my app.config appSettings' external file to point to a path using an environment variable? The below is not recognized:

<appSettings file="%AppData%\MyApp\MySettingsFile.config" />

However, absolute paths are recognized, even though they are not within the hierarchy of the output path of the assembly, such as:

<appSettings file="C:\MyApp\MySettingsFile.config" />
2
  • Environment variables are not supported. Commented Jul 10, 2017 at 16:05
  • 1
    Do you have a source for that? TY Commented Jul 10, 2017 at 16:06

2 Answers 2

2

You can do it as below:

If your changes in app.config as below:

<appSettings file="%AppData%\MyApp\MySettingsFile.config" />

then in .cs file, just expand the environment variable using Environment.ExpandEnvironmentVariables as below:

var pathConfig = ConfigurationManager.AppSettings["file"];
var expandedPath = Environment.ExpandEnvironmentVariables(pathConfig);

expandedPath will have the expanded path.

Sign up to request clarification or add additional context in comments.

Comments

1

"The path specified is relative to the main configuration file. For a Windows Forms application, this would be the binary folder (such as /bin/debug), not the location of the application configuration file. For Web Forms applications, the path is relative to the application root, where the web.config file is located."

https://msdn.microsoft.com/en-us/library/aa903313(v=vs.71).aspx

I'm guessing your scenario works when calling the full path because your MySettingsFile.config is still within your application heirarchy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.