gpt4 book ai didi

I was using Visual Studio 2019 on a WPF C# project for a win app. I found a way to embed all DLLs to the exe. This is not longer valid with VS2022(我在WPF C#项目的Win应用程序上使用了Visual Studio 2019。我找到了一种方法将所有DLL嵌入到可执行文件中。这在VS2022中不再有效)

转载 作者:bug小助手 更新时间:2023-10-28 13:07:22 25 4
gpt4 key购买 nike



I was using Visual Studio 2019 on a WPF C# project for a win app. This app was configure with old style not with SDK-Style projects. I wanted to have all DLLs files embedded into the executable so I can just have one single exe file.
This is not longer valid with Visual Studio 2022 and the new SDK-Style project. For some reason the same solution doesn't work there.

我在WPF C#项目的Win应用程序上使用了Visual Studio 2019。这个应用程序是用旧风格配置的,而不是SDK风格的项目。我希望将所有DLL文件嵌入到可执行文件中,这样我就可以只有一个EXE文件。这对于Visual Studio 2022和新的SDK样式项目不再有效。出于某种原因,同样的解决方案在那里行不通。


I tried the same thing I had in my previous project:

我尝试了我在上一个项目中做的相同的事情:


<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterResolveReferences">
<ItemGroup>
<EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
<LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Target>

Now with the VS 2022 and the SDK-Style project I needed to change a lot more thing in the .vcproj file to get the same functionality I have to get the single file application functionality.

现在有了VS2022和SDK风格的项目,我需要在.vcproj文件中更改更多内容才能获得与单文件应用程序功能相同的功能。


The Import tag doesn't work anymore because it says that it is adding the targets anyway so it is duplicated. I found a way to not to use it and have the Target to be at the end of the build phase.

导入标记不再起作用,因为它说无论如何都要添加目标,所以它被复制了。我找到了一种不使用它的方法,并将Target放在构建阶段的末尾。


<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="echo executing the post build event" />
<ItemGroup>
<EmbeddedResource Include="@(ReferenceCopyLocalPaths)"
Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
<LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Target>

The problem now is that, the embedded stuff is not working for some reason and I cannot find anywhere on the internet a solution for this problem. It is like every one is using the old style method and that's it. Maybe it is not possible anymore.

现在的问题是,由于某种原因,嵌入式的东西不起作用,我在互联网上找不到解决这个问题的方法。好像每个人都在用老方法,就是这样。也许这已经不可能了。


Here is a post from 14 years ago: Embedding DLLs in a compiled executable

这是14年前的一篇文章:在编译后的可执行文件中嵌入DLL


更多回答
优秀答案推荐

Costura fody still works if you want to just embed references, just download it in nuget packages.

如果你只想嵌入引用,Costura fody仍然有效,只需在Nuget包中下载它即可。


Just install it into your project and build your source, and it should embed all the reference DLLs into your executable. No extra steps needed.

只需将其安装到您的项目中并构建源代码,它应该会将所有引用DLL嵌入到您的可执行文件中。不需要额外的步骤。



Ok, I finally found the new way of doing things

好了,我终于找到了新的做事方式


To achieve the same effect as before, I dont need to add any extra code anywhere. Just by adding

为了达到和以前一样的效果,我不需要在任何地方添加任何额外的代码。只需添加


    <PropertyGroup>
<PublishSingleFile>true</PublishSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
</PropertyGroup>

To the .csproj and then publishing the project with the configuration Self-contained, solves the whole thing. Everything is included inside the .exe file.

到.csproj,然后使用自包含的配置发布项目,这就解决了整个问题。所有内容都包含在.exe文件中。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com