Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.config settings, environment variable as partial path

I'm new to tinkering with app.config and xml, and am currently doing some refactoring in some code I haven't written.
Currently we have a snippet which looks like this:

<setting name="FirstSetting" serializeAs="String">
  <value>Data Source=C:\Documents and Settings\All Users\ApplicationData\Company ...;Persist Security Info=False</value>

What I'd like to do is have it instead point to something like ${PROGRAMDATA}\Company\...

How can I achieve this, keeping in mind that PROGRAMDATA will not always point to C:\ProgramData ?

like image 544
Jean-Bernard Pellerin Avatar asked Apr 11 '26 09:04

Jean-Bernard Pellerin


2 Answers

use

Environment.ExpandEnvironmentVariables(stringFromConfig);

it replaces all existing environment variables in the string like %ProgramData% with the exact values.

like image 132
troYman Avatar answered Apr 13 '26 23:04

troYman


I didn't really want to change it in code as per the other responses, since that removes the purpose of having it as a config setting.

As it turns out, %ProgramData%\Company... is the proper way of using environment variables in this context.

like image 36
Jean-Bernard Pellerin Avatar answered Apr 13 '26 23:04

Jean-Bernard Pellerin