DEV Community

artydev
artydev

Posted on

Embed an Exe file in .NetCore

Sometimes you don't want your user to install an external exe which is needed by your program.
I have to use Pandoc.exe in one of my program. I have decided to embed it in my own exe and at runtime copy the resource to a tmp directory.
You could perhaps experience problems with you anti-virus.

Here is the code from How to embed exe or resource file in c# assembly :

using System;
using System.IO;
using System.Reflection;

namespace PandocEmbed
{
  class Program
  {
    private static void ReadResourceNameFromAssembly()
    {
      foreach (var resourceName in Assembly
        .GetExecutingAssembly()
        .GetManifestResourceNames())
      {
        Console.WriteLine(resourceName);
      }
    }

    static void Main(string[] args)
    {
      // From here you can read the name of your embeded resource.
      ReadResourceNameFromAssembly();

      string resourceName = "PandocEmbed.pandoc.exe";
      Assembly currentAssembly = Assembly.GetExecutingAssembly();
      FileStream outputFileStream = new FileStream(@"c:\temp\pandoc.exe", FileMode.OpenOrCreate);
      Stream res = currentAssembly.GetManifestResourceStream(resourceName);
      if (res != null)
      {
        res.CopyTo(outputFileStream);
        res.Close();
      }
      outputFileStream.Close();
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

Discussion (0)