Click here to Skip to main content
14,452,666 members

Run Executable from Resources in C#

Rate this:
3.80 (3 votes)
Please Sign up or sign in to vote.
3.80 (3 votes)
17 Jun 2015CPOL
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
Questionno extraction Pin
Wrangly23-Feb-18 1:38
MemberWrangly23-Feb-18 1:38 
QuestionSerial Number Pin
ledtech322-Jun-15 7:24
Memberledtech322-Jun-15 7:24 
AnswerRe: Serial Number Pin
Ahmed E Osama25-Jun-15 5:23
MemberAhmed E Osama25-Jun-15 5:23 
GeneralRe: Serial Number Pin
ledtech325-Jun-15 16:30
Memberledtech325-Jun-15 16:30 
QuestionFile Not Found Message is shown when u try to download Pin
SAM LIVE19-Jun-15 3:47
MemberSAM LIVE19-Jun-15 3:47 
AnswerRe: File Not Found Message is shown when u try to download Pin
Ahmed E Osama25-Jun-15 5:21
MemberAhmed E Osama25-Jun-15 5:21 
GeneralRe: File Not Found Message is shown when u try to download Pin
SAM LIVE8-Jul-15 1:02
MemberSAM LIVE8-Jul-15 1:02 
SuggestionNot much of a protection Pin
Tomas Takac17-Jun-15 22:18
MemberTomas Takac17-Jun-15 22:18 
GeneralRe: Not much of a protection Pin
Ahmed E Osama18-Jun-15 3:36
MemberAhmed E Osama18-Jun-15 3:36 
GeneralRe: Not much of a protection Pin
Tomas Takac18-Jun-15 12:02
MemberTomas Takac18-Jun-15 12:02 
GeneralRe: Not much of a protection Pin
Ahmed E Osama25-Jun-15 5:22
MemberAhmed E Osama25-Jun-15 5: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.

Tip/Trick
Posted 17 Jun 2015

Stats

27.9K views
927 downloads
10 bookmarked