C# 通过Costura.Fody把DLL合并到exe程序中

打包独立的exe程序有多种方法,这里只说Costura.Fody。

我们用VS发布应用程序可以借助Costura.Fody直接打包成一个独立的exe程序,但是一些非托管的做了几次都没打进去,最后成功了,这里记录一下。

首先安装Costura.Fody

C# 通过Costura.Fody把DLL合并到exe程序中_第1张图片
在这里插入图片描述

或者可以通过这里获取
https://github.com/Fody/Costura/tree/develop

我的版本是5.7.0安装好了后会自动生成FodyWeavers.xml

<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
	<Costura>
	</Costura>
</Weavers>

具体的配置可以参考上面的官方下载

非托管DLL

我的项目中使用了UnRAR64一个解压缩的DLL,是C++的非托管DLL,在项目编译的时候Costura并不能自动把这个DLL打入exe。

[DllImport("unrar64.dll")]

总结我失败的原因,需要几个步骤:
1,把UnRAR64.dll设置成嵌入的资源
2,把这个文件复制到目录Costura64下
3,需要在xml里配置非托管的文件名

如图:
C# 通过Costura.Fody把DLL合并到exe程序中_第2张图片

<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
	<Costura>
		<Unmanaged64Assemblies>
			UnRAR64
		</Unmanaged64Assemblies>
	</Costura>
</Weavers>

参考:
https://github.com/Fody/Costura/tree/develop
https://blog.csdn.net/qq_39200794/article/details/122146823

你可能感兴趣的:(C#,c#,开发语言,Costura.Fody)