如何将引用的项目导入exe? [英] How do I import referenced projects into exe?

查看:93
本文介绍了如何将引用的项目导入exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,它正在引用解决方案中的其他项目.当我构建它时,它将在Debug中复制那些dll.我想在exe中导入它们.如果我将它们添加到资源中,然后从那里加载,它们将不会更新.我丢失了对引用的DLL的更改.有什么方法可以构建它们并将其导入到每次构建的可执行文件中?

解决方案

以下是对我有用的解决方案:

http://www.hanselman.com/blog/MixingLanguagesInASingleAssemblyInVisualStudioSeamalyBuild ./p>

它会在每次构建后使用ILMerge合并程序集(如注释中所建议).我需要为.NET Framework 4更新.targets文件.以防万一有人需要它:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 
    <Target Name="AfterBuild"> 
        <CreateItem Include="@(ReferencePath)" Condition="'%(CopyLocal)'=='true' and '%(ReferencePath.IlMerge)'=='true'"> 
            <Output TaskParameter="Include" ItemName="IlmergeAssemblies"/> 
        </CreateItem> 
        <Message Text="MERGING: @(IlmergeAssemblies->'%(Filename)')" Importance="High" /> 
        <Exec Command="&quot;$(ProgramFiles)\Microsoft\Ilmerge\Ilmerge.exe&quot; /out:@(MainAssembly) &quot;@(IntermediateAssembly)&quot; @(IlmergeAssemblies->'&quot;%(FullPath)&quot;', ' ') /target:exe /targetplatform:v4,C:\Windows\Microsoft.NET\Framework64\v4.0.30319 /wildcards" /> 
    </Target> 
  <Target Name="_CopyFilesMarkedCopyLocal"/> 
</Project>

更新

尽管上述解决方案有效,但您可以使其更简单,并且不需要目标文件.您可以将ILMerge放在解决方案中的某个位置.然后在构建后从那里调用它.您只需ILMerge.exe,将其复制到/solutionDirectory/Tools这样的位置.在构建后事件命令行中编写命令.

$(SolutionDir)Tools\ILMerge.exe /out:"$(ProjectDir)bin\Debug\WindowsGUI.exe" "$(ProjectDir)obj\x86\Debug\WindowsGUI.exe" "$(SolutionDir)BusinessLayer\bin\Debug\BusinessLayer.dll" /target:exe /targetplatform:v4,"$(MSBuildBinPath)" /wildcards

构建后,您将获得带有嵌入式DLL的.exe,并且可以单独运行它.

I have a console application which is referencing other projects in solution. When I build it, it will copy those dlls in Debug. I want to import them in the exe. If I add them to resources then load from there, they are not updated. I lose the changes on referenced DLLs. Is there a way that I can build them and import them in the executable file on each build?

解决方案

Here's the solution that worked for me:

http://www.hanselman.com/blog/MixingLanguagesInASingleAssemblyInVisualStudioSeamlesslyWithILMergeAndMSBuild.aspx

It merges assemblies after each build with ILMerge (like suggested in comments). I needed to update .targets file for .NET Framework 4. In case anyone needs it:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 
    <Target Name="AfterBuild"> 
        <CreateItem Include="@(ReferencePath)" Condition="'%(CopyLocal)'=='true' and '%(ReferencePath.IlMerge)'=='true'"> 
            <Output TaskParameter="Include" ItemName="IlmergeAssemblies"/> 
        </CreateItem> 
        <Message Text="MERGING: @(IlmergeAssemblies->'%(Filename)')" Importance="High" /> 
        <Exec Command="&quot;$(ProgramFiles)\Microsoft\Ilmerge\Ilmerge.exe&quot; /out:@(MainAssembly) &quot;@(IntermediateAssembly)&quot; @(IlmergeAssemblies->'&quot;%(FullPath)&quot;', ' ') /target:exe /targetplatform:v4,C:\Windows\Microsoft.NET\Framework64\v4.0.30319 /wildcards" /> 
    </Target> 
  <Target Name="_CopyFilesMarkedCopyLocal"/> 
</Project>

Update

While the solution above works, you can make it simpler and you wouldn't need the targets files. You can put ILMerge somewhere in solution. Then call it from there after build. ILMerge.exe is all you need, copy it in somewhere like /solutionDirectory/Tools. Write a command in your post-build event command line.

$(SolutionDir)Tools\ILMerge.exe /out:"$(ProjectDir)bin\Debug\WindowsGUI.exe" "$(ProjectDir)obj\x86\Debug\WindowsGUI.exe" "$(SolutionDir)BusinessLayer\bin\Debug\BusinessLayer.dll" /target:exe /targetplatform:v4,"$(MSBuildBinPath)" /wildcards

After the build, you get the .exe with embedded DLLs and you can run it alone.

这篇关于如何将引用的项目导入exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆