c#:如何将exe文件嵌入资源中? [英] c#: How to embed exe file into resources?

查看:152
本文介绍了c#:如何将exe文件嵌入资源中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Costura.Fody.

I use Costura.Fody.

有一个应用程序Test.exe可以通过以下方式运行pocess internalTest.exe:

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

现在我需要向用户提供2个exe文件.

Now I need to provide 2 exe files to user.

是否可以嵌入internalTest.exe然后运行它?

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

推荐答案

将应用程序复制到解决方案中的某个文件夹中,该文件夹名为: 资源或EmbeddedResources等

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

这篇关于c#:如何将exe文件嵌入资源中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆