Click here to Skip to main content
12,892,596 members (46,116 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 8 Apr 2014

InnoSetup, Optimize your .NET Application with ngen - Part II

, 8 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Run ngen to optimize your application during the installation process


This is an update to my previous article of running ngen while installing a .NET application.

Unlike the older article, this installation will not bring any ngen file within the installer, and make safe that ngen is the same file that is installed in the user system.


In this tip, we'll configure InnoSetup to run ngen directly from the .NET framework working directory.

The main purpose is to locate the right path of the .NET Framework needed using the windows registry.

In this example, I have used .NET 4.0, but this example should work also for 3.5 and 4.5.

Btw, a similar solution might be used also for .NET version < 3.5, because older version probably have the same installation directory and we can use a static path.

Using the Code

As the previous article provided, we already have the code needed to check if .NET Framework required is installed, so let's assume we use the function IsRequiredDotNetDetected to check whatever our .NET is installed.

Then, we write a function that retrieves .NET install path:

function GetV4NetDir(version: string) : string;

var regkey, regval  : string;


    // in case the target is 3.5, replace 'v4' with 'v3.5'

    // for other info, check out this link 


    regkey := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'

    RegQueryStringValue(HKLM, regkey, 'InstallPath', regval);

    result := regval;

On the [Run] section, we add this line:

Filename: {code:GetV4NetDir}ngen.exe; Parameters: "install ""{app}\{#MyAppExeName}"""; 
StatusMsg: Optimizing performance for your system ...; Flags: runhidden; Check: CheckFramework;

The code above will run ngen.exe with install parameter (directly from the framework installation directory) on our main executable.

If we want to do stuff more clean, we can also add this line on the [UninstallRun] section.

Filename: {code:GetV4NetDir}ngen.exe; Parameters: "uninstall ""{app}\{#MyAppExeName}"""; 
StatusMsg: Removing native images and dependencies ...; Flags: runhidden; Check: CheckFramework;  

In the same way, ngen will remove native images of our application before doing an uninstall.

Points of Interest

With just a few lines of script, we are capable of making a smart setup installer, and even more a smart installation, saving space for native architecture libraries.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Fabrizio Stellato
Software Developer (Senior)
Italy Italy
Creator of:
Impulse Media Player
Audio Pitch & Shift
Ultimate Music Tagger
Modern Log Viewer
Pitch Tuner
Modern Audio Tagger
Win Log Inspector
Win Log Analyzer

You may also be interested in...

Comments and Discussions

QuestionInnoSetup OracleXCopySetup.msi Pin
fikri18-Apr-14 3:12
memberfikri18-Apr-14 3:12 
Suggestion64-bit optimization and config files Pin
Werner van Deventer8-Apr-14 20:15
memberWerner van Deventer8-Apr-14 20:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 8 Apr 2014
Article Copyright 2014 by Fabrizio Stellato
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid