C# 將外部exe檔存在程式內,調用時再提取出來使用

流程

  1. 將外部exe檔變成內部資源檔
  2. 利用Properties.Resources.<資源名>並搭配方法可以在運行時將其提取出來

提取內部exe的方法

public static void GenerateExe(byte[] FileBytes, string DestinationPath)
        {
            try
            {
                //1) Fetch Exe file content from Resources
                byte[] memoryFile = FileBytes;

                //2) Create file to be deleted complete execution
                FileStream aFile = new FileStream(DestinationPath, FileMode.Create, FileAccess.Write, FileShare.None, 20000, FileOptions.None);

                //3) Write Exe file content
                aFile.Write(memoryFile, 0, memoryFile.Length);
                aFile.Flush();
                aFile.Close();
                Thread.Sleep(100);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                DeleteExeFile();
            }

        }

調用方法

string fullPath = Application.StartupPath + @"\aria2c.exe";
ExeManager.GenerateExe(Properties.Resources.aria2c, fullPath);

完整程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Generate_Exe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void GenerateExe(byte[] FileBytes, string DestinationPath)
        {
            string fullPath = Application.StartupPath + @"\" + @"aria2.exe";
            fullPath = DestinationPath;
            try
            {
                //1) Fetch Exe file content from Resources
                byte[] memoryFile = FileBytes;

                //2) Create file to be deleted complete execution
                FileStream aFile = new FileStream(DestinationPath, FileMode.Create, FileAccess.Write, FileShare.None, 20000, FileOptions.None);

                //3) Write Exe file content
                aFile.Write(memoryFile, 0, memoryFile.Length);
                aFile.Flush();
                aFile.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                File.Delete(fullPath);
            }
        }
        private void Extract_Click(object sender, EventArgs e)
        {
            if (!File.Exists(Application.StartupPath + @"\aria2c.exe"))
            {
                GenerateExe(Properties.Resources.aria2c, Application.StartupPath + @"\aria2c.exe");
                Output_Info.Text = "提取成功";
            }
            else
            {
                Output_Info.Text = "檔案已存在請先刪除";
            }
            
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if(File.Exists(Application.StartupPath + @"\aria2c.exe"))
            {
                try
                {
                    File.Delete(Application.StartupPath + @"\aria2c.exe");
                    Output_Info.Text = "刪除成功";
                }
                catch(IOException f)
                {
                    Output_Info.Text = f.Message;
                }
            }
            else
            {
                Output_Info.Text = "檔案不存在";
            }
        }
    }
}

原始碼下載

https://mega.nz/#!HWBwQKDS!V_yWy5drJCCm9V9hOTHApL8MMgr-2AffskkIod8SjG4

Last modification:December 8, 2019
If you think my article is useful to you, please feel free to appreciate