rec*_*ive 10 installer visual-studio-2010 winforms web-config-transform
我正在使用此问题的解决方案,以便App.config在Winforms项目中应用配置更改.我还有一个项目的安装程序项目,它创建一个可安装的*.msi文件.问题是,安装程序中捆绑的配置文件是原始的,未转换的配置文件.因此,即使构建的winforms项目的配置文件已应用了所有正确的转换,我们也无法在生产安装程序中获取生产连接字符串.
有没有办法强制安装程序项目使用项目构建的输出?
首先:app.config使用该Primary output选项无法使Setup Project指向另一个文件.所以,我的解决方案将是一个围绕工作.我希望你发现它在你的情况下很有用.
概述:
基本思路是:
app.config从安装项目中删除强制;app.config手动添加指向的文件;vdproj文件,并将其更改为与已转换的app.config的实际输出相匹配.一些缺点是:
开始工作吧:
1)转到Setup Project,选择Primary Output对象,右键单击并转到Properties.在那里你会发现Exclude Filter...添加一个过滤器*.config,所以它将删除硬编码的app.config.
2)在解决方案资源管理器中右键单击您的安装项目 - >添加 - >文件...选择任何以.config.结尾的文件.
3)下载MSBuild社区任务项目,我推荐msi安装程序.
4)卸载你的项目(csproj)并用这个替换其他问题的代码:
码:
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
<!-- Generate transformed app config in the intermediate directory -->
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
<!-- Force build process to use the transformed configuration file from now on. -->
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
<PropertyGroup>
<SetupProjectPath>$(MSBuildProjectDirectory)\$(IntermediateOutputPath)$(TargetFileName).config</SetupProjectPath>
</PropertyGroup>
<!-- Change the following so that this Task can find your vdproj file -->
<FileUpdate Files="$(MSBuildProjectDirectory)\..\Setup1\Setup1.vdproj"
Regex="(.SourcePath. = .8:).*\.config(.)"
ReplacementText="$1$(SetupProjectPath.Replace(`\`,`\\`))$2" />
<FileUpdate Files="$(MSBuildProjectDirectory)\..\Setup1\Setup1.vdproj"
Regex="(.TargetName. = .8:).*\.config(.)"
ReplacementText="$1$(TargetFileName).config$2" />
</Target>
Run Code Online (Sandbox Code Playgroud)
5)必须更改以前的代码,以便它可以找到您的vdproj文件.我在代码中添加了注释,指出了您需要进行更改的位置.
现在,每次构建主项目时,MSBuild都会更改安装项目,以便它使用正确的app.config文件.它可能有缺点,但这种解决方案可以抛光并变得更好.如果您需要发表评论,我会尽快回复.
我使用的资源
需要MSBuild 4.0,因为我需要使用String的Replace函数,将单个"\"替换为路径中的双"\".有关在MSBuild中使用函数的详细信息,请参阅 MSBuild属性函数.
我在另一个问题中了解了FileUpdate任务.官方项目是MSBuild社区任务项目.
这两个主题对我的发现非常重要:
小智 8
我发现的另一个解决方案是不使用转换,而是只有一个单独的配置文件,例如app.Release.config.然后将此行添加到csproj文件中.
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<AppConfig>App.Release.config</AppConfig>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
这将强制部署项目在打包时使用正确的配置文件.
我结合了以下最佳答案,在不使用任何外部工具的情况下获得了一个完全有效的解决方案:
1. 设置 App.Config 转换
来源:https : //stackoverflow.com/a/5109530
简而言之:
为每个构建配置手动添加额外的 .config 文件并编辑原始项目文件以包含它们,类似于:
<Content Include="App.config" />
<Content Include="App.Debug.config" >
<DependentUpon>App.config</DependentUpon>
</Content>
<Content Include="App.Release.config" >
<DependentUpon>App.config</DependentUpon>
</Content>
Run Code Online (Sandbox Code Playgroud)
然后在项目文件的末尾,就在结束</project>标记之前,包含以下 XML :
<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>
Run Code Online (Sandbox Code Playgroud)
最后编辑额外的 .config 文件以包含每个构建配置的相应转换:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!-- transformations here-->
</configuration>
Run Code Online (Sandbox Code Playgroud)
2. 在安装项目中包含适当的 .config
首先,在主项目的 postbuild 事件中添加一个命令,将适当的转换后的 .config 文件移动到一个中性位置(例如主bin\目录):
copy /y "$(TargetDir)$(TargetFileName).config" "$(ProjectDir)bin\$(TargetFileName).config"
Run Code Online (Sandbox Code Playgroud)
(来源:https : //stackoverflow.com/a/26521986)
打开安装项目并单击“主要输出...”节点以显示属性窗口。在那里,添加一个 ExludeFilter"*.config"以排除默认(未转换)的 .config 文件。
(来源:https : //stackoverflow.com/a/6908477)
最后将转换后的 .config 文件(来自 postbuild 事件)添加到安装项目(添加 > 文件)。
完毕。
您现在可以自由添加构建配置和相应的配置转换,并且您的安装项目将始终包含适用于活动配置的 .config。
| 归档时间: |
|
| 查看次数: |
5578 次 |
| 最近记录: |