Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: How to embed exe file into resources?

I use Costura.Fody.

There is an app Test.exe which runs pocess internalTest.exe this way:

      ProcessStartInfo prcInfo = new ProcessStartInfo(strpath)
        {
            CreateNoWindow = false,
            UseShellExecute = true,
            Verb = "runas",
            WindowStyle = ProcessWindowStyle.Normal
        };
        var p = Process.Start(prcInfo);

Now I need to provide 2 exe files to user.

Is it possible to embed internalTest.exe and then run it?

like image 567
ZedZip Avatar asked Sep 26 '22 08:09

ZedZip


1 Answers

Copy the application to a folder within your solution called something like: Resources or EmbeddedResources etc

Set the Build Action to 'Embedded Resource' for that application from the solution explorer.

Now the application will be embedded within your application at build time.

In order to access it at 'Run Time' you need to extract it to a location where you can execute it from.

using (Stream input = thisAssembly.GetManifestResourceStream("Namespace.EmbeddedResources.MyApplication.exe")) 
            {

                byte[] byteData = StreamToBytes(input); 

            }


        /// <summary>
        /// StreamToBytes - Converts a Stream to a byte array. Eg: Get a Stream from a file,url, or open file handle.
        /// </summary>
        /// <param name="input">input is the stream we are to return as a byte array</param>
        /// <returns>byte[] The Array of bytes that represents the contents of the stream</returns>
        static byte[] StreamToBytes(Stream input)
        {

            int capacity = input.CanSeek ? (int)input.Length : 0; //Bitwise operator - If can seek, Capacity becomes Length, else becomes 0.
            using (MemoryStream output = new MemoryStream(capacity)) //Using the MemoryStream output, with the given capacity.
            {
                int readLength;
                byte[] buffer = new byte[capacity/*4096*/];  //An array of bytes
                do
                {
                    readLength = input.Read(buffer, 0, buffer.Length);   //Read the memory data, into the buffer
                    output.Write(buffer, 0, readLength); //Write the buffer to the output MemoryStream incrementally.
                }
                while (readLength != 0); //Do all this while the readLength is not 0
                return output.ToArray();  //When finished, return the finished MemoryStream object as an array.
            }

        }

Once you have your byte[] for the application inside your parent application, you can use

System.IO.File.WriteAllBytes();

to save the byte array to your hard drive with the file name you want.

You can then use the following to start your application. You may want to use logic to determine if the application exists there already, and try to remove it if it does. If it does exist, just run it without saving over it.

System.Diagnostics.Process.Start(<FILEPATH HERE>); 
like image 167
Baaleos Avatar answered Oct 02 '22 15:10

Baaleos