Search code examples
c#xnamonogame

Embed dlls and contents into exe in monogame


In other C# projects such as Window Forms and WPF, I can just copy the built exe to other environment with .net and run without errors.

However, in MonoGame, the exe file requires a lot of dlls and needs the content folder to run successfully.

Is there a way to include the dlls and contents inside the exe?


Solution

  • Actually, it can be achieved by 2 steps.

    1. Embedding the dlls.
    2. Including the contents.

    Embedding the dlls

    This can be easily achieved by installing Costura.Fody using NuGet.

    Type the following line to the Package Manager Console

    Install-Package Costura.Fody
    

    Source: Embedding DLLs in a compiled executable.

    Including the contents

    1. Copy your compiled content files (.xnb files and other contents) to your Content folder.
    2. Go to the project property page and add your compiled content files to your resources.
    3. Replace the code below:

      public Game1()
      {
          graphics = new GraphicsDeviceManager(this);
          Content.RootDirectory = "Content";
      }
      

      with

      public Game1()
      {
          graphics = new GraphicsDeviceManager(this);
          ResourceContentManager resxContent;
          resxContent = new ResourceContentManager(Services, Resources.ResourceManager);
          Content = resxContent;
      }
      
    4. Finished! You can now load content using the old method

      Content.Load<Texture2D>("Image1");
      

    Source: Loading Content Within a Game Library