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).
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
- 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
MyManager = new ExeManager(ExeSecure.Properties.Resources.TrendOnl, Application.StartupPath);
- 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;
- Getting the current HDD serial: it is best done by the
ManagementObject as follows:
ManagementObject disk = new ManagementObject
("win32_logicaldisk.deviceid=\"" + drive + ":\"");
HDDNum = int.Parse(disk["VolumeSerialNumber"].ToString(),
- 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);