Create C# executable with mkbundle on windows

Learn create c# executable with mkbundle on windows with practical examples, diagrams, and best practices. Covers c#, mono, mkbundle development techniques with visual explanations.

Create C# Executables on Windows with Mono's mkbundle

Hero image for Create C# executable with mkbundle on windows

Learn how to package your C# applications into standalone executables for Windows using Mono's mkbundle tool, simplifying deployment and distribution.

Deploying .NET applications, especially those built with Mono, can sometimes be challenging due to runtime dependencies. Mono's mkbundle tool offers a powerful solution by allowing you to create a single, self-contained executable that includes your application, its dependencies, and a minimal Mono runtime. This article will guide you through the process of using mkbundle on Windows to create standalone C# executables.

Understanding mkbundle and its Benefits

mkbundle is a utility provided by the Mono project that takes a .NET assembly (your compiled C# application) and bundles it, along with any referenced assemblies and a subset of the Mono runtime, into a single native executable. This eliminates the need for the target machine to have Mono installed separately, greatly simplifying deployment.

Key benefits include:

  • Simplified Deployment: Distribute a single .exe file without worrying about runtime installations.
  • Reduced Dependencies: The bundled executable contains everything it needs to run.
  • Cross-Platform Potential: While this guide focuses on Windows, mkbundle can be used on other platforms supported by Mono.
  • Version Control: You control the exact Mono runtime version bundled with your application.
flowchart TD
    A[C# Source Code] --> B[C# Compiler (csc.exe)]
    B --> C[Application.exe (IL Assembly)]
    C --> D[mkbundle.exe]
    D --"Includes Mono Runtime & Dependencies"--> E[Standalone Executable (.exe)]
    E --> F[Target Windows Machine]

The mkbundle process for creating a standalone executable.

Prerequisites and Setup

Before you can use mkbundle, you need to have Mono installed on your Windows machine. The mkbundle tool is part of the Mono SDK. Ensure you download the correct version for Windows.

  1. Install Mono for Windows: Download and install the latest stable version of Mono from the official Mono Project website. Choose the "Mono for Windows" installer.
  2. Verify Installation: After installation, open a command prompt and type mono --version. You should see output indicating the installed Mono version.
  3. Locate mkbundle: The mkbundle.exe utility is typically found in the bin directory of your Mono installation (e.g., C:\Program Files\Mono\bin). It's a good idea to add this directory to your system's PATH environment variable for easier access, or navigate to it directly in your command prompt.

Bundling Your C# Application

Once Mono is set up, you can proceed to bundle your application. Let's assume you have a simple console application named MyConsoleApp.exe that you've compiled. For this example, we'll use a basic "Hello, World!" application.

First, create a simple C# console application:

// Program.cs
using System;

public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello from mkbundled C# App!");
        Console.ReadKey(); // Keep console open
    }
}

Compile this application using csc.exe (if you have .NET SDK installed) or mcs.exe (Mono C# compiler):

csc Program.cs
# OR
mcs Program.cs

This will generate Program.exe in the same directory. Now, you can use mkbundle.

1. Navigate to your application directory

Open a command prompt and change the directory to where your compiled Program.exe is located.

2. Run mkbundle command

Execute the mkbundle command with the appropriate options. The most common usage involves specifying the output executable name and the input assembly.

mkbundle -o MyBundledApp.exe Program.exe --machine-config --config --static

Let's break down the options:

  • -o MyBundledApp.exe: Specifies the output filename for the bundled executable.
  • Program.exe: The main assembly of your application.
  • --machine-config: Includes the machine.config file, which is often necessary for many applications.
  • --config: Includes the application's configuration file (e.g., App.config if present).
  • --static: This is crucial. It tells mkbundle to statically link the Mono runtime, creating a truly standalone executable. Without this, it might still look for a Mono installation.

3. Test the bundled executable

After mkbundle completes, you will find MyBundledApp.exe in your directory. You can now copy this single file to another Windows machine (even one without Mono installed) and run it. It should execute just like a native Windows application.

Handling Additional Dependencies

If your application relies on other custom assemblies (DLLs) or specific NuGet packages, you need to include them in the mkbundle command. You can list them after your main executable.

For example, if Program.exe depends on MyLibrary.dll:

mkbundle -o MyBundledApp.exe Program.exe MyLibrary.dll --machine-config --config --static

For more complex dependency trees, you might need to list all direct and indirect dependencies. A common strategy is to copy all required DLLs into a single lib folder and then reference them from there, or use a tool like ildasm to inspect your assembly's references.

mkbundle -o MyComplexApp.exe MyComplexApp.exe Dependency1.dll Dependency2.dll AnotherLibrary.dll --machine-config --config --static

Bundling an application with multiple custom dependencies.