URGENT PLEASE - Reading Environment Variable from Config file

admin

Administrator
Staff member
Hi,

The requirement in brief is to read an environment variable from a Config File.

To explain further...
Every class that is used in the application that I am developing needs to read the value from App.Config. The application is developed in C#.

For instance, the App.Config has:
<appSettings>
<add key = "source" value = "C:\ABC" />
<appSettings>

In the code however I am aware that the value will be read as follows:

string sourceVal=
CustomConfigurationSettings.ConfigurationSettings.AppSettings["source"];
Console.WriteLine(sourceVal);

OUTPUT:

C:\ABC

When installed in different machines the value of the source will be changed and used.

If suppose SOURCE is declared as an environment variable which is set to the path "C:\ABC" then in that case, this can be read as follows in the code:

Console.WriteLine(Environment.GetEnvironmentVariable("SOURCE"));

OUTPUT:

C:\ABC

But the current requirement is such that the value of the key will be an environment variables. So that all App.Config will have a standard values and wherever the application is used, and only the environment variables needs to be set accordingingly.

So how am I to declare the value attribute in the appSettingf of App.Config to be an environment variable and when that value is being read how will the code know that it has to read the value of the Environment Variable?

For instance,
if SOURCE be the environment variable set to C:\ABC:

<appSettings>
<add key = "source" value = "SOURCE" />
<appSettings>

When the value from the appSettings is retrieved :
string sourceVal=
CustomConfigurationSettings.ConfigurationSettings.AppSettings["source"];
Console.WriteLine(sourceVal);

Output:

SOURCE

but my requirement is it should print C:\ABC

I tried something like
<appSettings>
<add key = "source" value = %SOURCE% />
<appSettings>

But it throws an error when I try to retrieve the value.

Please let me know at the earliest how this should be done.

Thanks in advance...
 
Back
Top