简体   繁体   中英

C# VS.NET 2008 Changing settings per configuration

Is there a way to have different application settings per each build configuration?

I would like to be able to batch build a few configurations with different settings, instead of having to change the settings and build, rinse repeat.

Thanks in advance!

I don't know much about the appsettings architecture (I've never really used it), but you can define different values for constants using a bit of MSBuild magic.

Create two .cs files, Constants1.cs and Constants2.cs (or name them after your configurations).

In each file, define a class called Constants (or whatever) -- but do it as if each file were the only definition (ie. use the same class name). Typically, this should just define public static readonly fields -- do not use const , as that can get you into trouble with partial builds.

Now Unload your project file and Edit it. Find the entries that look like this:

<Compile Include="Constants1.cs" />
    <Compile Include="Constants2.cs" />

and change them like so:

<Compile Include="Constants1.cs" Condition="'$(Configuration)'=='Debug'" />
    <Compile Include="Constants2.cs" Condition="'$(Configuration)'=='Release'" />

Finally Save and Reload your project. Now only one of the files will actually be built at a time, depending on your build configuration.

You could add a prebuild or postbuild task to the proj, you have access to the ConfigurationName from there. Would be fairly easy to do something like "copy Web.config.debug Web.config"

除了所有这些,MS承诺在VS 2010中添加此功能。

What do you mean under 'application settings' ? Project's property for each configuration such as Debug or Release? Or defirent app.conf file for each of them?

If first, you can create a number of configurations with suitable settings and use Batch Build to build them by turn. http://msdn.microsoft.com/en-us/library/169az28z.aspx

or as Google Ninja said, use pre-build task: del Web.config app.config copy Web.config.Debug Web.config (create a number of configuration files before it)

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