復制代碼 代碼如下:
ngen install filepath
復制代碼 代碼如下:
Ngen uninstall filepath
復制代碼 代碼如下:
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;
namespace NgenInstaller
{
[RunInstaller(true)]
public partial class NgenInstaller : System.Configuration.Install.Installer
{
public NgenInstaller()
{
InitializeComponent();
}
public override void Install(IDictionary stateSaver)
{
NgenFile(InstallTypes.Install);
}
public override void Uninstall(IDictionary savedState)
{
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["assemblypath"];
string appDir = Path.GetDirectoryName(exePath);
int i = 1;
do {
string fileKey = "ngen" + i;
//需要生成本機映象的程序集名字,配置在ngen1...5,6的配置中
if (Context.Parameters.ContainsKey(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);
}
}
}
添加安裝程序之后右擊安裝項目添加項目輸出,如下圖所示
在添加項目輸出的對話框中選擇Winforms項目和剛建的類庫。
然后右擊安裝項目選擇視圖---自定操作打開自定義操作窗口,添加一個名字為“Ngen生成本機映象”的自定義操作,在添加操作時要選擇NgenInstaller項目輸出 如下圖:
然后將自定義操作的名字修改為“Ngen生成本機代碼”,如下圖所示
然后右擊新建的自定義操作,選擇屬性窗口,在屬性窗口的CustomActionData屬性中添加需要操作的程序集,注意CustomActionData是一個鍵值對,每個鍵值對以/開始
完成這一步就差不多了,你可以編譯一下整個解決方案。運行安裝項目生成的安裝包。
6. 運行安裝包,如果一切正常的話就做了本機映象生成了,可以通過ngen display filePath命令來驗證native本機影響是否安裝正常,如下命令行輸出
新聞熱點
疑難解答
圖片精選