Zach Ross-Clyne
Zach Ross-Clyne

Reputation: 799

Set Environment Variable during Setup

Is there any way to change a value of a setting in the Settings.settings file of a C# project during the installation stage. I have 2 projects in 1 solution, 1 is the actual project, and the other is the Setup and Deployment > Setup Project project.

Upvotes: 0

Views: 301

Answers (1)

Andrew
Andrew

Reputation: 2335

You might already have an App.Config file. If not make sure try clicking the "Show all files" icon in Solution Explorer. Then, if you still do not have one right-click your project and select "Add new Item" there is a item type of config file. For more info see MSDN's documentation on adding a config file

Basically it's an XML file in which you add values you want to use to configure your application by using:

    <configuration>
       <appSettings>
          <add key="My Key" value="Some Value"/>
       </appSettings>
    </configuration>

You access the config file by including System.Configuration in your code and then use the following:

string myValue = ConfigurationManager.AppSettings["myKey"];

This way you can control values through deployments - we have config files for our dev, UAT and live environments with things such as database/web service locations and items specific to the deployment. It's a much easier way of doing things IMO.

Upvotes: 3

Related Questions