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 using the just-in-time
(JIT) compiler to compile the original
assembly.
Unfortunately, you still need the libraries from the framework in order to run your program. There's no feature that I know of with the MS .Net framework SDK that allows you to compile all the required files into a single executable
You can use ngen.exe to generate a native image but you still have to distribute the original non-native code as well, and it still needs the framework installed on the target machine.
The nature of .NET is to be able to install apps that have been compiled to MSIL, then either by JIT or Ngen, MSIL is compiled to native code and stored locally in a cache. It was never intended on generating a true native .exe that can be run independently of the .NET framework.
Maybe there's some hack that does this, but it doesn't sound safe to me. There are too many dynamics that require the framework, such as: dynamic assembly loading, MSIL code generation, etc.
I have tested several of them and at this moment the only one that supports .NET 3.5 and also has a great virtualization stack is XenocodePostbuild
With ngen you still need to have the .NET framework installed but using a tool as such all your managed code is compiled into native code so you can deploy it to machines without the framework presence.
Currently it is only available for Windows Store Apps. It performs single component linking. So .NET Framework libraries are statically linked into your app. Everything is compiled to native and IL assemblies are no longer deployed. Apps do not run against CLR but a stripped down, optimized runtime called Managed Runtime (Mrt.dll)
As stated above, NGEN used a mix compilation model and relied on IL and JIT for dynamic scenarios. .NET Native does not utilise JIT but it does support various dynamic scenarios. Code authors would need to utilize Runtime Directives to provide hints to the .NET Native compiler on the dynamic scenarios they wish to support.
As some of the other answers here have mentioned, you can use the .NET Native tool to compile your app to native machine code. Unlike those answers, however, I will explain how to do it.
Steps:
Install the dotnet CLI (command line interface) tool, which is part of the new .NET Core toolchain. We'll use this to compile our app; you can find a good article about it here.
Open up a shell prompt and cd to the directory of your app.
Type this:
dotnet compile --native
That's it! When you're done, your app will be compiled down to a single binary, like this:
It'll be a standalone executable; no PDBs, assemblies, or config files included (hooray!).
Alternatively, if you want an even faster program, you can run this:
dotnet compile --native --cpp
That will optimize your program using the C++ code generator (as opposed to RyuJIT), so your app is even more optimized for AOT scenarios.
You can find more info on this at the dotnet CLI GitHub repo.
2019 Answer: Use dotnet/corert. It can compile .NET Core projects into standalone .exe files. No dependencies (except for system libraries like kernel32.dll). I bet this is exactly what the OP need.
From its GitHub home page:
The CoreRT compiler can compile a managed .NET Core application into a native (architecture specific) single-file executable that is easy to deploy. It can also produce standalone dynamic or static libraries that can be consumed by applications written in other programming languages.
The main reason to compile into Native is to secure your codes, otherwise the MSIL compiled is like deploying the source codes in the client's machine.
NGEN compiles into native but also need to deploy IL codes, this purpose is just to reduce the startup time but it is also useless.
CoreRt is alpha version and working only with simple helloworld type apps.
.Net Core compiles into single executable files but it is also not native exe, this is just a zipped file of IL codes and it will unzip the codes into temp folder while running.
My simple question from Microsoft is, if RyuJIT can compile IL into native on the fly then why not you can compile the same IL ahead-of-time (AOT).
This technology is a bit limiting, since you cannot rely on reflection too much, but overall you can compile range of application with it. Web apps, WinForms apps, console apps.