AnthonyFG
AnthonyFG

Reputation: 68

How do I create a runtime envrionmental variable from a build time environmental variable?

I am not sure this is possible but I want to create a runtime environmental variable that gets evaluated at build time.

The idea being that three developers can use different servers for testing and not have to change it every time the project is checked out.

This is in C# .net

Upvotes: 1

Views: 48

Answers (1)

oscilatingcretin
oscilatingcretin

Reputation: 10959

I do stuff like that sometimes.

<connectionStrings>
    <add name="BobServer" connectionString="bob's connection string" />
    <add name="MaryServer" connectionString="mary's connection string" />
    <add name="JimServer" connectionString="jim's connection string" />
</connectionStrings>

string 
    ConnectionName = Environment.UserName + "Server",
    ConString = ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString;

using (SqlConnection con = new SqlConnection(ConString))
{
}

If Environment.UserName is Bob, it will use the BobServer connection string. If it's Mary, it will use MaryServer. You'd probably need to make some modifications, but this should help put you in the right direction.

Upvotes: 3

Related Questions