首页 > 其他 > 详细

动态加载DLL

时间:2016-09-04 10:15:28      阅读:211      评论:0      收藏:0      [点我收藏+]

最近给客户的一个winform发布更新程序,但是因为当初固定了程序集列表,所以新加入的dll不会被客户下载,这就有个问题,新的application必须有那个dll才能运行,否则肯定报错。还好确定了一下,如果代码没有运行到某个dll的对象,那个dll是不会被AppDomain加载的,这样,只要在main入口,把dll文件生成就可以保证后面的程序运行正常了。

首先,把dll文件作为签入的资源文件,一起编译成exe,然后在main函数里面,把资源文件生成本地dll

基本代码是这样:

        static void Main(string[] args)
        {
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "SharpCompress.dll") == false)
            {
                Assembly assembly = typeof(Program).Assembly;
                //读取资源文件
                System.IO.Stream streamSmall = assembly.GetManifestResourceStream("AppUpdate.SharpCompress.dll");
                byte[] content = new byte[streamSmall.Length];
                streamSmall.Read(content,0,content.Length);
                //生成dll
                File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + "SharpCompress.dll", content);
            }
        }

 

动态加载DLL

原文:http://www.cnblogs.com/IWings/p/5838652.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!