Embed & Execute Resource Files (exe, mp3, txt, etc.)

We shall now learn how to embed files internally into a program. The file can be any type i.e. with any extension. The fun part is that the file's visibility to the user can be controlled & can also be moved to any location within the hard disk. Thus we can execute exe's on a completely different process/thread. If we have programs that were built before & need reuse or desired to be run parallel, this is the technique.The process is explained as follows:

1. Right click on the project name and select Properties.

2. Click on the Resources tab on the Left.

3. Click on Add Resource >> Add Existing File.

4. Choose the file location on your HDD & u will see the file available in the Solution Explorer.

5. You will be able to access this file using a funtion shown below:
  
string exePath = Application.StartupPath.ToString()+"\\FileName.exe";

public void ExtractResource(string resource)
        {
            Stream stream = GetType().Assembly.GetManifestResourceStream(resource);
            byte[] bytes = new byte[(int)stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            File.WriteAllBytes(exePath, bytes);
            System.Diagnostics.Process.Start(exePath);
         }

Use this function as explained

*Call the ExtractResource() function whenever you want to run an embedded resource file.

*The string 'resource' is the exact file name including the extension(.exe, .txt, etc).

*The string 'path' is where the resource will be extracted before it is run. The resource will be extracted only when needed/called. 

*If the File is not an exe then the default program will be used to launch the file.

*The above function actually extracts the resource in the execution path/folder itself(the value of exePath). The value can be anything suitable or even custom. The selection can be made by the choose folder dialog box.

*The System.Diagnostics.Process.Start(exePath) function executes an executable file from HDD whose location is given in the parameter.