Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C# 5.0
Tip/Trick

Run Executable from Resources in C#

Rate me:
Please Sign up or sign in to vote.
3.80/5 (3 votes)
17 Jun 2015CPOL2 min read 41.7K   1.2K   10   11
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:
    C#
    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:
    C#
    lblCurrentserial.Text = Properties.Settings.Default.HDDSerial;
  3. Getting the current HDD serial: it is best done by the ManagementObject as follows:
    C#
    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:
    C#
    Process x = Process.Start(rPath);
                    x.WaitForExit();
                    File.Delete(rPath);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionno extraction Pin
Wrangly23-Feb-18 0:38
Wrangly23-Feb-18 0:38 
QuestionSerial Number Pin
ledtech322-Jun-15 6:24
ledtech322-Jun-15 6:24 
The Volume Serial Number is different than the Hard drive Serial Number, The number that the manufacture assigns to the drive.
AnswerRe: Serial Number Pin
Ahmed E Osama25-Jun-15 4:23
Ahmed E Osama25-Jun-15 4:23 
GeneralRe: Serial Number Pin
ledtech325-Jun-15 15:30
ledtech325-Jun-15 15:30 
QuestionFile Not Found Message is shown when u try to download Pin
SAM LIVE19-Jun-15 2:47
SAM LIVE19-Jun-15 2:47 
AnswerRe: File Not Found Message is shown when u try to download Pin
Ahmed E Osama25-Jun-15 4:21
Ahmed E Osama25-Jun-15 4:21 
GeneralRe: File Not Found Message is shown when u try to download Pin
SAM LIVE8-Jul-15 0:02
SAM LIVE8-Jul-15 0:02 
SuggestionNot much of a protection Pin
Tomas Takac17-Jun-15 21:18
Tomas Takac17-Jun-15 21:18 
GeneralRe: Not much of a protection Pin
Ahmed E Osama18-Jun-15 2:36
Ahmed E Osama18-Jun-15 2:36 
GeneralRe: Not much of a protection Pin
Tomas Takac18-Jun-15 11:02
Tomas Takac18-Jun-15 11:02 
GeneralRe: Not much of a protection Pin
Ahmed E Osama25-Jun-15 4:22
Ahmed E Osama25-Jun-15 4:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.