Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Run Executable from Resources in C#

0.00/5 (No votes)
17 Jun 2015 1  
Run an executable that is inside the resources in C#

Introduction

Sometimes, we need to distribute some third party EXEs with our application and yet keep them hidden and not reusable. To do so, you may include the EXE file during design time as a resource in C#.

This tip will show how to do so, and how to execute at runtime also how to use this technique to protect a third party application from running on a different machine than the current (Copy protection).

Background

I was in need of protecting a certain old product, and the only way I could think of is to lock its EXE either by MAC address or HDD serial of current machine, and I worked on developing a self contained application that can launch the legacy App once it is on the right HDD, and prevents running if copied to a different machine. So in this small tip, you get to learn how to get HDD serial, MAC Address, you will also know how use resources and file manipulation during runtime, in addition to learning about Settings and storing/recalling user settings during runtime in the easiest way as it is supported in .NET.

Using the Code

  1. Using a resource at runtime: since the resource is added as a binary file so the best direct way to use it is via Properties.Resources as a Byte[] array:
    MyManager = new ExeManager(ExeSecure.Properties.Resources.TrendOnl, Application.StartupPath);
  2. To store and retrieve user information such as HDD serial during runtime, I used the settings in C# that I had already created in design time, and was able to use as read/write settings in runtime:
    lblCurrentserial.Text = Properties.Settings.Default.HDDSerial;
  3. Getting the current HDD serial: it is best done by the ManagementObject as follows:
    ManagementObject disk = new ManagementObject
    ("win32_logicaldisk.deviceid=\"" + drive + ":\"");
                //bind our management object
                disk.Get();
                //return the serial number
                HDDNum = int.Parse(disk["VolumeSerialNumber"].ToString(), 
                System.Globalization.NumberStyles.HexNumber);
                return HDDNum.ToString();
  4. Running and then deleting EXE after execution completed: normally, after writing the byte array to disk from resources to and EXE file, you can start it using Process.Start, but then you want to make sure this file is deleted after it completes execution, so you wait for completion and then delete as follows:
    Process x = Process.Start(rPath);
                    x.WaitForExit();
                    File.Delete(rPath);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here