Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force the installer project to use the .config file from the built solution instead of the original one?

I am using the solution to this question in order to apply configuration changes to App.config in a Winforms project. I also have an installer project for the project that creates an installable *.msi file. The problem is, the config file bundled in the installers is the original, un-transformed config file. So we're not getting the production connection strings in the production installer even though the config file for the built winforms project has all the correct transformations applied.

Is there any way to force the installer project to use the output of project build?

like image 393
recursive Avatar asked Jul 26 '11 17:07

recursive


People also ask

Where is web config file in Visual Studio 2022?

config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ folder. The default settings that are contained in the Machine.


2 Answers

I combined the best of the following answers to get a fully working solution without using any external tools at all:

1. Setup App.Config transformations

Source: https://stackoverflow.com/a/5109530

In short:

Manually add additional .config files for each build configuration and edit the raw project file to include them similar to this:

<Content Include="App.config" />
<Content Include="App.Debug.config" >
  <DependentUpon>App.config</DependentUpon>
</Content>
<Content Include="App.Release.config" >
  <DependentUpon>App.config</DependentUpon>
</Content>

Then include the following XML at the end of the project file, just before the closing </project> tag:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
  <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
  <ItemGroup>
    <AppConfigWithTargetPath Remove="app.config" />
    <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
      <TargetPath>$(TargetFileName).config</TargetPath>
    </AppConfigWithTargetPath>
  </ItemGroup>
</Target>

Finally edit the additional .config files to include the respective transformations for each build configuration:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!-- transformations here-->
</configuration>

2. Include the appropriate .config in the setup project

First, add a command in the postbuild event of your main project to move the appropriate transformed .config file to a neutral location (e.g. the main bin\ directory):

copy /y "$(TargetDir)$(TargetFileName).config" "$(ProjectDir)bin\$(TargetFileName).config"

(Source: https://stackoverflow.com/a/26521986)

Open the setup project and click the "Primary output..." node to display the properties window. There, add an ExludeFilter "*.config" to exclude the default (untransformed) .config file.

(Source: https://stackoverflow.com/a/6908477)

Finally add the transformed .config file (from the postbuild event) to the setup project (Add > File).

Done.

You can now freely add build configurations and corresponding config transforms and your setup project will always include the appropriate .config for the active configuration.

like image 69
marsze Avatar answered Sep 27 '22 23:09

marsze


Another solution I've found is not to use the transformations but just have a separate config file, e.g. app.Release.config. Then add this line to your csproj file.

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    <AppConfig>App.Release.config</AppConfig>
  </PropertyGroup>

This will force the deployment project to use the correct config file when packaging.

like image 45
Alec Avatar answered Sep 28 '22 00:09

Alec