下(类似C:\Windows\Microsoft.NET\Framework\v4.0.30319)有一个ngen.exe工具,就是做这件事儿的。这个程序是一个控制台程序,最常用的使用方法如下:
using
System;
using System.Collections;
using System.Collections.Generic;
using System.
componentModel;
using System.
configuration.Install;
using System.Linq;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
&nbs
p; namespace NgenInstaller
{
[RunInstaller(true)]
p
ublic partial class NgenInstaller : System.Configuration.Install.Installer
{
public NgenInstaller()
{
In
ITializeComponent();
}
public
override void Install(IDictionary
stateSaver)
{
NgenFile(InstallTy
PEs.Install);
}
public override void Uninstall(IDictionary save
dstate)
{
NgenFile(InstallTypes.Uninstall);
}
private enum InstallTypes
{
Install,
Uninstall
}
private void NgenFile(InstallTypes options)
{
string
envDir = RuntimeEnvironment.GetRuntimeDirectory();
string ngenPath = Path
.COMbine(envDir, "ngen.exe");
string exePath = Context.Parameters["as
SEMblypath"];
string appDir = Path.GetDirectoryName(exePath);
int i = 1;
do {
string fileKey = "ngen" + i;
//需要生
成本机映象的程序集名字,配置在ngen1
...5,6的配置中
if (Context.Parameters.Cont
ainsKey(fileKey))
{
string ngenFileName = Context.Parameters["ngen" + i];
string fileFullName = Path.Combine(appDir, ngenFileName);
string
argument = (options
== InstallTypes.Install ? "install" : "uninstall") + " \"" + fileFullName + "\"";
Process ngenProcess = new Process();
ngenProcess.StartInfo.FileName = ngenPath;
ngenProcess.StartInfo.
arguments = argument;
ngenProcess.StartInfo.CreateNoWindow = true;
ngenProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ngenProcess.Start();
ngenProcess.WaitForExit();
i++;
}
else {
break;
}
}
while (true);
}
}
}