收藏 分销(赏)

C#中用ILMerge将所有引用DLL及exe文件打成一个exe文件.doc

上传人:仙人****88 文档编号:7215340 上传时间:2024-12-28 格式:DOC 页数:17 大小:654.50KB 下载积分:10 金币
下载 相关 举报
C#中用ILMerge将所有引用DLL及exe文件打成一个exe文件.doc_第1页
第1页 / 共17页
C#中用ILMerge将所有引用DLL及exe文件打成一个exe文件.doc_第2页
第2页 / 共17页


点击查看更多>>
资源描述
C#中用ILMerge将所有引用的DLL和exe文件打成一个exe文件,有图解 2011-01-29 13:35 转载自 zhuyachao1989 最终编辑 zhuyachao1989 今天做了一个软件,想发布的时候才发现调用的类没几个,就像把它们都跟EXE文件打包在一起,以后复制去别的地方用也方便,于是上网搜了一下,发现网上大部分都是用ILMerge实现的,于是也自己试了一下,不过网上都没有详细的步骤演示,我就花点时间做了个教程,方便以后再有人想打包自己的程序,有篇文章可以参考,好了废话少说,马上开始: 1.先到 2.下载后是安装: 3.不到一分钟就可以安装完毕: 4.我是安装在I盘下ILMerge目录下,以下是安装后的文件,就一个ILMerge.exe文件而已,我们等会就是用它打包程序的. 5.为了演示,我重新建了个工程,最后生成的是 Main.exe 和 newDll文件,其中newDll是Main.exe 中要引用的Dll文件,为了演示方便,我将它们都复制到了ILMerge的目录下,现在我们就试着用ILMerge将newDll和Main.exe文件打包起来吧. 6.然后进入dos窗口,进入ILMerge目录,然后执行下面代码: I:\ILMerge\ILMerge.exe /ndebug /target:winexe /out:newclient.exe MainExe.exe /log newDll.dll 7.ILMerge运行时的窗口,此时它正在努力的将Main.exe和newDll文件捆绑在一起: 8.DOS窗口关闭后,我们在去ILMerge目录下看看,此时已经生成好了我们想要的newclient.exe文件,它就是Main.exe和newDll.dll的结合体啦: 以上实验在WIN7下.net framework3.5 平台,vs2008环境中测试通过 大家都知道网卡的MAC地址可以从DOS窗口中通过输入"ipconfig /all"命令运行结果获得,那么这个问题的具体内容是:  在C#中运行一个dos命令,并截取相关输出、输出流。  具体代码如下: C# code tbResult.Text = "";  ProcessStartInfo start = new ProcessStartInfo("Ping.exe"); //设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到  //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe  start.Arguments = txtCommand.Text; //设置命令参数  start.CreateNoWindow = true; //不显示dos命令行窗口  start.RedirectStandardOutput = true; //  start.RedirectStandardInput = true; //  start.UseShellExecute = false; //是否指定操作系统外壳进程启动程序  Process p=Process.Start(start);  StreamReader reader = p.StandardOutput; //截取输出流  string line = reader.ReadLine(); //每次读取一行  while (!reader.EndOfStream)  {  tbResult.AppendText(line+" ");  line = reader.ReadLine();  }  p.WaitForExit(); //等待程序执行完退出进程  p.Close(); //关闭进程  reader.Close(); //关闭流 C#中用ILMerge将所有引用的DLL和exe文件打成一个exe文件 2011-01-12 23:38 转载于: 今天做了一个软件,想发布的时候才发现调用的类没几个,就像把它们都跟EXE文件打包在一起,以后复制去别的地方用也方便,于是上网搜了一下,发现网上大部分都是用ILMerge实现的,于是也自己试了一下,不过网上都没有详细的步骤演示,我就花点时间做了个教程,方便以后再有人想打包自己的程序, 有篇文章可以参考,好了废话少说,马上开始:   1.先到   2.下载后是安装:   3.不到一分钟就可以安装完毕:   4.我是安装在I盘下ILMerge目录下,以下是安装后的文件,就一个ILMerge.exe文件而已,我们等会就是用它打包程序的.   5.为了演示,我重新建了个工程,最后生成的是 Main.exe 和 newDll文件,其中newDll是Main.exe 中要引用的Dll文件,为了演示方便,我将它们都复制到了ILMerge的目录下,现在我们就试着用ILMerge将newDll和Main.exe文件打包起来吧.   6.然后进入dos窗口,进入ILMerge目录,然后执行下面代码:   I:\ILMerge\ILMerge.exe /ndebug /target:winexe  /out:newclient.exe MainExe.exe  /log newDll.dll   7.ILMerge运行时的窗口,此时它正在努力的将Main.exe和newDll文件捆绑在一起:   8.DOS窗口关闭后,我们在去ILMerge目录下看看,此时已经生成好了我们想要的newclient.exe文件,它就是Main.exe和newDll.dll的结合体啦:   方法1:把相关的第三方dll作为程序资源嵌入到EXE中,在程序运行的时候,从资源文件中输出到程序执行目录即可  (图1:示例项目,ThirdPartydlldemo.dll作为第三方资源.Build Action属性设置为" Embedded Resource") 然后在Program.cs里面声明个静态构造函数,在该方法里面把第三方dll输出到程序目录,这样在调用第三方dll方法的时候,相关环境已经初始化完毕了.          private static void ExtractResourceToFile(string resourceName, string filename)     {        if (!System.IO.File.Exists(filename))          using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))          usng (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))          {             byte[] b = new byte[s.Length];             s.Read(b, 0, b.Length);             fs.Write(b, 0, b.Length);         }     }复制代码 这样就ok了. 方法2:是用Ilmerge这个微软提供的工具,直接把相关的dll嵌入到目标exe中,而且程序运行时候,不像方法1会把相关的dll输出到可执行目录下,它直接让.net运行时到程序的资源中去找相关的dll引用,以下是Ilmerge的介绍:  This document describes the ILMerge utility which merges multiple .NET assemblies into a single assembly. However, some .NET assemblies may not be able to be merged because they may contain features such as unmanaged code. I would highly recommend using peverify (the .NET Framework SDK tool) on the output of ILMerge to guarantee that the output is verifiable and will load in the .NET runtime. ILMerge is packaged as a console application. But all of its functionality is also accessible programmatically. Note that Visual Studio does allow one to add an executable as a reference, so you can write a client that uses ILMerge as a library. ILMerge takes a set of input assemblies and merges them into one target assembly. The first assembly in the list of input assemblies is the primary assembly. When the primary assembly is an executable, then the target assembly is created as an executable with the same entry point as the primary assembly. Also, if the primary assembly has a strong name, and a .snk file is provided, then the target assembly is re-signed with the specified key so that it also has a strong name. Note that anything that depended upon any of the names of the input assemblies, e.g., configuration files, must be updated to refer instead to the name of the target assembly. Any Win32 Resources in the primary assembly are copied over into the target assembly. There are many options that control the behavior of ILMerge. These are described in the next section. Ilmerge 相关的命令行参数是: ilmerge [/lib:directory]* [/log[:filename]] [/keyfile:filename [/delaysign]] [/internalize[:filename]] [/t[arget] library|exe|winexe)] [/closed] [/ndebug] [/ver:version] [/copyattrs [/allowMultiple]] [/xmldocs] [/attr:filename] ([/targetplatform:<version>[,<platformdir>]]|v1|v1.1|v2|v4) [/useFullPublicKeyForReferences] [/zeroPeKind] [/wildcards] [/allowDup[:typename]]* [/allowDuplicateResources] [/union] [/align:n] /out:filename <primary assembly> [<other assemblies>...] 其中目标exe或者程序集,要放在输入的程序集里面的第一位置,其他dll放在它之后.其中/out:参数是必须的,其他参数可以参考文档 如图1 示例,命令行参数是  ilmerge EmbeddedDLL2ExeDemo.exe ThirdPartyDllDemo.dll /ndebug /out:EmbeddedDll2Ex  eDemo.exe 这样既可,该方法比方法1更完美,不过这个Ilmerge 在使用的时候还有一些不足的地方,比如/ver:version,这个参数设置后没有效果; ilmerge就用自己把它引用到的两个dll嵌软到它自身里面了. 方法3:使用.Net混淆器都附带这样的功能,可以把多个dll整合到一个可执行文件中。 最终编辑 foshanfdhjp 废话就不多说了,直接进入主题吧 用VS2005建立一个windows项目,取名test 引用dll文件 编写代码,正常引用dll里的类库, 同时在test项目添加资源文件(该文件就是刚才引用的dll文件) VS2005会自动生成引用代码,我这里引用的是IrisSkin2.dll internal static byte[] IrisSkin2 ...{             get ...{                 object obj = ResourceManager.GetObject("IrisSkin2", resourceCulture);                 return ((byte[])(obj));              }          } 然后在Main(program.cs)函数里加入代码 static void Main()         ...{              Application.EnableVisualStyles();              Application.SetCompatibleTextRenderingDefault(false);             string path = Application.StartupPath + "\";             string dllFileName = "IrisSkin2.dll";             //******加载IrisSkin2.dll******             if (!File.Exists(path + dllFileName))   //文件不存在             ...{                  FileStream fs = new FileStream(path + dllFileName, FileMode.CreateNew, FileAccess.Write);                 byte[] buffer = GetData.Properties.Resources.IrisSkin2;//{GetData是命名空间}                  fs.Write(buffer, 0, buffer.Length);                  fs.Close();              }             //*****************************                           Application.Run(new GDForm());          } 编译test项目,生成exe文件, 然后删除引用的dll文件(注意是先编译,再删除) 复制该exe文件就可以在别的地方运行了(不用dll,运行EXE会自动生成DLL文件)
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服