Winform 程序中dll程序集嵌入exe可执行文件

时间:2021-07-16 07:36:15

关于这方面,Google一下有很多方法,参考:

http://blog.csdn.net/astonqa/article/details/7300856

但按照以上的方法我并没有成功,于是继续找到了一篇更详细的步骤说明:

http://adamthetech.com/2011/06/embed-dll-files-within-an-exe-c-sharp-winforms/

按照以上链接的步骤去做,代码也不用修改,直接粘贴到构造函数中即可。

再次附上相应的代码:

C#版:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = new AssemblyName(args.Name).Name + ".dll";
string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName)); using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, , assemblyData.Length);
return Assembly.Load(assemblyData);
}
};

VB.NET版:

AddHandler AppDomain.CurrentDomain.AssemblyResolve,
Function(sender, args)
Dim resourceName As String = New AssemblyName(args.Name).Name + ".dll"
Dim resource As String = Array.Find(Me.[GetType]().Assembly.GetManifestResourceNames(), Function(element) element.EndsWith(resourceName)) Using stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource)
Dim assemblyData As [Byte]() = New [Byte](stream.Length - ) {}
stream.Read(assemblyData, , assemblyData.Length)
Return Assembly.Load(assemblyData)
End Using
End Function