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" />
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.
"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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With