简体   繁体   中英

How Can I Use Configuration Replacements for C# Console Application

Is there a way that I can make configuration replacements for a C# console application in Visual Studio 2010 similar to how it is done through a deploy project for an asp.net/web application project? I have different connection strings and mail settings that I use for test and prod environments so I need an easy way to switch settings when making deployments.

Yes you can. You should create two folders in your project like "config1" and "config2". Put in each an app.config file that suits the settings you need. Create two project configurations like "Release1" and "Release2". Open your csproj file in an editor and look for the lines that include both files. In each xml node you can check what is the current selected configuration, and tell VS whether to compile or not each file. Couldn't find an example for that but try to look for "multiple app.config".

Edit: Just found this and it looks great. An addin for VS to use transformations in app.config like web.config http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx

You can also use #if DEBUG as shown below:

using System;

namespace IfDebug
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString;

#if DEBUG
            connectionString = "a";
#else
            connectionString = "b";
#endif
            Console.WriteLine(connectionString);

            Console.ReadLine();
        }
    }
}

If the Solution Configuration dialogue box is set to Debug , 'a' will be printed, if not 'b' will be printed.

Microsoft explains it better here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM