ILMerge是将 C#制作的WinForm应用程序打包成一个.exe文件(不是打包成安装文件啊!),
首先安装ILMerge,到百度搜索这个这个关键字就会有很多说明、使用方法、安装下载。
安装以后,需找到你安装ILMerge的目录,因为操作ILMerge需要在DOC下。
但ILMerge不支持WPF程序,下面二步操作即可实现ILMerge一样的功能,并且是集成在VS开发环境的中。
1.将WPF主程序(EXE工程)卸载,在编辑工程文件,在XML最后部分添加上下面一段配置
...
<
Import
Project
=
"$(MSBuildToolsPath)\Microsoft.CSharp.targets"
/>
<
Target
Name
=
"AfterResolveReferences"
>
<
ItemGroup
>
<
EmbeddedResource
Include
=
"@(ReferenceCopyLocalPaths)"
Condition
=
"'%(ReferenceCopyLocalPaths.Extension)' == '.dll'"
>
<
LogicalName
>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</
LogicalName
>
</
EmbeddedResource
>
</
ItemGroup
>
</
Target
>
</Project> 这最后一行上面
2.添加一个入口程序文件,如WINFROM里的 main,
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Reflection;
namespace
WpfApplication1
{
public
class
Program
{
[STAThreadAttribute]
public
static
void
Main()
{
var assemblies =
new
Dictionary<
string
, Assembly>();
var executingAssembly = Assembly.GetExecutingAssembly();
var resources = executingAssembly.GetManifestResourceNames().Where(n => n.EndsWith(
".dll"
));
foreach
(
string
resource
in
resources)
{
using
(var stream = executingAssembly.GetManifestResourceStream(resource))
{
if
(stream ==
null
)
continue
;
var bytes =
new
byte
[stream.Length];
stream.Read(bytes, 0, bytes.Length);
try
{
assemblies.Add(resource, Assembly.Load(bytes));
}
catch
(Exception ex)
{
System.Diagnostics.Debug.Print(
string
.Format(
"Failed to load: {0}, Exception: {1}"
, resource, ex.Message));
}
}
}
AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
{
var assemblyName =
new
AssemblyName(e.Name);
var path =
string
.Format(
"{0}.dll"
, assemblyName.Name);
if
(assemblies.ContainsKey(path))
{
return
assemblies[path];
}
return
null
;
};
App.Main();
}
}
}
3. 在WPF项目属性里将启动项设为 上面的的program,再次编译下,看看你的程序是不是变大了。
4.将程序复制到其他目录或电脑进行测试,能正常运行,这里不需要复制引用的其他第三方组件。