开发者

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开发者_JAVA技巧</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 ?


use

Environment.ExpandEnvironmentVariables(stringFromConfig);

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


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.


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));


Considering the PROGRAMDATA is an environment variable, you can access using C#

String EnviromentPath = System.Environment.GetEnvironmentVariable("PROGRAMDATA", EnvironmentVariableTarget.Machine);


you could use

var programDataValue = Environment.GetEnvironmentVariable("PROGRAMDATA");

if it comes from an environment variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜