Embedding DLL's into .exe in in Visual C# 2010

前端 未结 8 2313
臣服心动
臣服心动 2020-12-14 10:31

I\'m working on a C# program that uses iTextSharp.dll and WebCam_Capture.dll. When I build the program, it creates executable in the debug folder and it also copies these tw

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 10:58

    Add this anonymous function code on the top of our application constructor. This will add dll from embedded resource in same project.

    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, 0, assemblyData.Length);
            return Assembly.Load(assemblyData);
        }
    };
    

提交回复
热议问题