Introduction
This small application allows the user to select an EXE from a dialog by which it will use the Native Image Generator (Ngen.exe).
As most who use Ngen know, it is a command line tool which is not very intuitive. I got tired of typing the commands in, and created this app to simply select the EXE.
Using the code
The code is as easy as it gets. You select the button and choose your program's EXE.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
namespace NgenInstaller
{
public partial class MainForm : Form
{
private Process ngenProcess = new Process();
private string runtimeStr =
RuntimeEnvironment.GetRuntimeDirectory();
private string ngenStr;
public MainForm()
{
InitializeComponent();
ngenStr = Path.Combine(runtimeStr, "ngen.exe");
}
private void btnSelect_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "EXE files (*.exe)|*.exe";
ofd.FilterIndex = 1;
ofd.RestoreDirectory = true;
DialogResult dr = DialogResult.OK;
if (ofd.ShowDialog() == dr)
{
ngenProcess.StartInfo.UseShellExecute = false;
ngenProcess.StartInfo.FileName = ngenStr;
ngenProcess.StartInfo.Arguments =
string.Format("install \"{0}\"", ofd.FileName);
ngenProcess.Start();
ngenProcess.WaitForExit();
}
}
}
}
Points of interest
The Native Image Generator (Ngen.exe) is a tool that improves the performance of managed applications. Ngen.exe creates native images, which are files containing compiled processor-specific machine code, and installs them into the native image cache on the local computer. The runtime can use native images from the cache instead of using the just-in-time (JIT) compiler to compile the original assembly.
For more information, see: http://msdn.microsoft.com/en-us/library/6t9t5wcf(VS.80).aspx.