C#将DLL嵌入到exe当中

我就直接上内容了。虽然能看懂,知道原理。可是本身仍是没法独立写出来。看来还须要多学习啊。。c#


原文地址,程序我本身从新作了一遍ide


首先,你须要一个解决方案,而且包含已经引用的第三方的dll学习

wKioL1Z9AQDxGno8AACbzPrnnmM730.png


2.打开Properties下的Resources,将须要包含的dll添加进去spa

wKiom1Z9ATrQdUPxAAB8LHj81SM256.png

wKioL1Z9ApGw4zr1AAA2qopdAMc585.png

wKiom1Z9ATrBi8UTAABxEU6DI4A093.png


3.在Form1.cs中添加代码.net

  public Form1() {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            InitializeComponent();
}
 private System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
            string dllName = args.Name.Contains(",") ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", "");
            dllName = dllName.Replace(".", "_");
            if(dllName.EndsWith("_resources")) return null;
            System.Resources.ResourceManager rm = new System.Resources.ResourceManager(GetType().Namespace + ".Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
            byte[] bytes = (byte[])rm.GetObject(dllName);
            return System.Reflection.Assembly.Load(bytes);
 }


4.从新生成解决方案便可,而后你能够在bin文件夹中找到已经嵌入dll的exeorm