复制代码 代码如下:
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本机影响是否安装正常,如下命令行输出

新闻热点
疑难解答
图片精选