c#: How to embed exe file into resources?

那年仲夏 提交于 2020-08-02 12:28:33

问题


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?


回答1:


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>); 


来源:https://stackoverflow.com/questions/35482994/c-how-to-embed-exe-file-into-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!