收藏 分销(赏)

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

上传人:仙人****88 文档编号:7215340 上传时间:2024-12-28 格式:DOC 页数:17 大小:654.50KB
下载 相关 举报
C#中用ILMerge将所有引用DLL及exe文件打成一个exe文件.doc_第1页
第1页 / 共17页
C#中用ILMerge将所有引用DLL及exe文件打成一个exe文件.doc_第2页
第2页 / 共17页
C#中用ILMerge将所有引用DLL及exe文件打成一个exe文件.doc_第3页
第3页 / 共17页
C#中用ILMerge将所有引用DLL及exe文件打成一个exe文件.doc_第4页
第4页 / 共17页
C#中用ILMerge将所有引用DLL及exe文件打成一个exe文件.doc_第5页
第5页 / 共17页
点击查看更多>>
资源描述

1、C#中用ILMerge将所有引用的DLL和exe文件打成一个exe文件,有图解2011-01-29 13:35转载自 zhuyachao1989最终编辑 zhuyachao1989今天做了一个软件,想发布的时候才发现调用的类没几个,就像把它们都跟EXE文件打包在一起,以后复制去别的地方用也方便,于是上网搜了一下,发现网上大部分都是用ILMerge实现的,于是也自己试了一下,不过网上都没有详细的步骤演示,我就花点时间做了个教程,方便以后再有人想打包自己的程序,有篇文章可以参考,好了废话少说,马上开始:1.先到2.下载后是安装:3.不到一分钟就可以安装完毕:4.我是安装在I盘下ILMerge目录下

2、,以下是安装后的文件,就一个ILMerge.exe文件而已,我们等会就是用它打包程序的.5.为了演示,我重新建了个工程,最后生成的是 Main.exe 和 newDll文件,其中newDll是Main.exe 中要引用的Dll文件,为了演示方便,我将它们都复制到了ILMerge的目录下,现在我们就试着用ILMerge将newDll和Main.exe文件打包起来吧.6.然后进入dos窗口,进入ILMerge目录,然后执行下面代码:I:ILMergeILMerge.exe /ndebug /target:winexe /out:newclient.exe MainExe.exe /log newD

3、ll.dll7.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# codetbResult.Text =

4、; ProcessStartInfo start = new ProcessStartInfo(Ping.exe); /设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到/如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exestart.Arguments = txtCommand.Text; /设置命令参数start.CreateNoWindow = true; /不显示dos命令行窗口start.RedirectStandardOutput = true; /start.RedirectStandardInput = true; /start.UseSh

5、ellExecute = 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(); /关

6、闭流C#中用ILMerge将所有引用的DLL和exe文件打成一个exe文件2011-01-12 23:38转载于:今天做了一个软件,想发布的时候才发现调用的类没几个,就像把它们都跟EXE文件打包在一起,以后复制去别的地方用也方便,于是上网搜了一下,发现网上大部分都是用ILMerge实现的,于是也自己试了一下,不过网上都没有详细的步骤演示,我就花点时间做了个教程,方便以后再有人想打包自己的程序, 有篇文章可以参考,好了废话少说,马上开始:1.先到2.下载后是安装:3.不到一分钟就可以安装完毕:4.我是安装在I盘下ILMerge目录下,以下是安装后的文件,就一个ILMerge.exe文件而已,我们

7、等会就是用它打包程序的.5.为了演示,我重新建了个工程,最后生成的是 Main.exe 和 newDll文件,其中newDll是Main.exe 中要引用的Dll文件,为了演示方便,我将它们都复制到了ILMerge的目录下,现在我们就试着用ILMerge将newDll和Main.exe文件打包起来吧.6.然后进入dos窗口,进入ILMerge目录,然后执行下面代码:I:ILMergeILMerge.exe /ndebug /target:winexe /out:newclient.exe MainExe.exe /log newDll.dll7.ILMerge运行时的窗口,此时它正在努力的将M

8、ain.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方法的时候,相关

9、环境已经初始化完毕了. privatestaticvoidExtractResourceToFile(stringresourceName,stringfilename)if(!System.IO.File.Exists(filename)using(System.IO.Streams=System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)usng(System.IO.FileStreamfs=newSystem.IO.FileStream(filename,System

10、.IO.FileMode.Create)byteb=newbytes.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

11、 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

12、 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 me

13、rges 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 stro

14、ng 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 a

15、ssembly.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

16、 /target library|exe|winexe) /closed /ndebug /ver:version /copyattrs /allowMultiple /xmldocs /attr:filename (/targetplatform:,|v1|v1.1|v2|v4) /useFullPublicKeyForReferences /zeroPeKind /wildcards /allowDup:typename* /allowDuplicateResources /union /align:n /out:filename .其中目标exe或者程序集,要放在输入的程序集里面的第一位

17、置,其他dll放在它之后.其中/out:参数是必须的,其他参数可以参考文档如图1 示例,命令行参数是ilmerge EmbeddedDLL2ExeDemo.exe ThirdPartyDllDemo.dll /ndebug /out:EmbeddedDll2ExeDemo.exe这样既可,该方法比方法1更完美,不过这个Ilmerge 在使用的时候还有一些不足的地方,比如/ver:version,这个参数设置后没有效果;ilmerge就用自己把它引用到的两个dll嵌软到它自身里面了.方法3:使用.Net混淆器都附带这样的功能,可以把多个dll整合到一个可执行文件中。最终编辑 foshanfdhj

18、p废话就不多说了,直接进入主题吧用VS2005建立一个windows项目,取名test引用dll文件编写代码,正常引用dll里的类库,同时在test项目添加资源文件(该文件就是刚才引用的dll文件)VS2005会自动生成引用代码,我这里引用的是IrisSkin2.dllinternal static byte IrisSkin2 .get .object obj = ResourceManager.GetObject(IrisSkin2, resourceCulture);return (byte)(obj); 然后在Main(program.cs)函数里加入代码static void Mai

19、n(). 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文件)

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

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

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

关于我们      便捷服务       自信AI       AI导航        获赠5币

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

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

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

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服