Reputation: 12670
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
?
Upvotes: 3
Views: 10723
Reputation: 1808
use
Environment.ExpandEnvironmentVariables(stringFromConfig);
it replaces all existing environment variables in the string like %ProgramData% with the exact values.
Upvotes: 4
Reputation: 12670
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.
Upvotes: 3
Reputation: 108957
you could use
var programDataValue = Environment.GetEnvironmentVariable("PROGRAMDATA");
if it comes from an environment variable.
Upvotes: 0
Reputation: 1015
Considering the PROGRAMDATA
is an environment variable, you can access using C#
String EnviromentPath = System.Environment.GetEnvironmentVariable("PROGRAMDATA", EnvironmentVariableTarget.Machine);
Upvotes: 0
Reputation: 941635
Yes, write it just like that in your setting. Then just substitute ${PROGRAMDATA} at runtime:
var setting = Properties.Settings.Default.FirstSetting;
setting = setting.Replace("${PROGRAMDATA)",
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
Upvotes: 1