配置设置,环境变量为部分路径

时间:2022-05-29 23:06:21

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:

我对修改app.config和xml很陌生,目前正在对一些我还没有编写的代码进行重构。目前我们有这样一个片段:

<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\...

我想做的是让它指向${PROGRAMDATA}\公司\…

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

我如何实现这一点,记住编程数据并不总是指向C:\编程数据?

5 个解决方案

#1


2  

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.

结果显示,%ProgramData%\Company…是在此上下文中使用环境变量的正确方法。

#2


2  

use

使用

Environment.ExpandEnvironmentVariables(stringFromConfig);

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

它将字符串中的所有现有环境变量(如%ProgramData%)替换为确切的值。

#3


1  

Yes, write it just like that in your setting. Then just substitute ${PROGRAMDATA} at runtime:

是的,像这样写在你的背景里。然后在运行时替换${PROGRAMDATA}:

        var setting = Properties.Settings.Default.FirstSetting;
        setting = setting.Replace("${PROGRAMDATA)", 
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

#4


0  

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

考虑到编程数据是一个环境变量,您可以使用c#访问它

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

#5


0  

you could use

您可以使用

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

if it comes from an environment variable.

如果它来自环境变量。

#1


2  

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.

结果显示,%ProgramData%\Company…是在此上下文中使用环境变量的正确方法。

#2


2  

use

使用

Environment.ExpandEnvironmentVariables(stringFromConfig);

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

它将字符串中的所有现有环境变量(如%ProgramData%)替换为确切的值。

#3


1  

Yes, write it just like that in your setting. Then just substitute ${PROGRAMDATA} at runtime:

是的,像这样写在你的背景里。然后在运行时替换${PROGRAMDATA}:

        var setting = Properties.Settings.Default.FirstSetting;
        setting = setting.Replace("${PROGRAMDATA)", 
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

#4


0  

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

考虑到编程数据是一个环境变量,您可以使用c#访问它

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

#5


0  

you could use

您可以使用

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

if it comes from an environment variable.

如果它来自环境变量。