C# 从Resources资源中启动应用程序,应用程序简单加壳,导出Resources中的资源为文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Packers
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        //[STAThread]
        //static void Main()
        //{
        //    Application.EnableVisualStyles();
        //    Application.SetCompatibleTextRenderingDefault(false);
        //    Application.Run(new Form1());
        //}

        //[STAThread]
        //static void Main(string[] args)
        //{
        //    Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Packers.easyIcon.exe");
        //    byte[] bs = new byte[stream.Length];
        //    stream.Read(bs, 0, (int)stream.Length);
        //    Assembly asm = Assembly.Load(bs);
        //    MethodInfo info = asm.EntryPoint;
        //    ParameterInfo[] parameters = info.GetParameters();
        //    if ((parameters != null) && (parameters.Length > 0))
        //        info.Invoke(null, (object[])args);
        //    else
        //        info.Invoke(null, null);
        //}

        [STAThread]
        static void Main(string[] args)
        {
            // 从附件启动应用程序,应用程序简单加壳
            byte[] rar = Properties.Resources.dat.ToArray<byte>();
            Assembly.Load(rar).EntryPoint.Invoke(null, null);
        }
    }
}
将要加壳的应用程序重命名,并添加到Resources中:

[STAThread]
static void Main(string[] args)
{
    // 从附件启动应用程序,应用程序简单加壳
    byte[] rar = Properties.Resources.dat.ToArray<byte>();
    Assembly.Load(rar).EntryPoint.Invoke(null, null);
}
示例项目: http://git.oschina.net/scimence/Packers


从Resources中,导出资源为文件:

/// <summary>
/// 导出资源为文件示例
/// </summary>
static void exportFile()
{
    Byte[] array = Properties.Resources.easyIcon.ToArray<Byte>();  // easyIcon.exe添加到Resources中对应的资源
    SaveFile(array, @"D:\easyIcon.exe");                           // 导出Resources中的资源为文件
}

/// <summary>
/// 保存Byte数组为文件
/// </summary>
private static void SaveFile(Byte[] array, string path)
{
    // 创建输出流
    System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create);

    //将byte数组写入文件中
    fs.Write(array, 0, array.Length);
    fs.Close();
}

附录:

/// <summary>
/// 更新文件逻辑,判定文件是否处于运行状态,关闭并删除后,创建新的文件
/// </summary>
private static void outPutFile(Byte[] array, string pathName)
{
    // 若文件正在运行,则从进程中关闭
    string fileName = System.IO.Path.GetFileName(pathName);
    KillProcess(fileName);

    // 删除原有文件
    if (System.IO.File.Exists(pathName)) System.IO.File.Delete(pathName);

    // 保存新的文件
    SaveFile(array, pathName);
}

/// <summary>
/// 关闭名称为processName的所有进程
/// </summary>
public static void KillProcess(string processName)
{
    Process[] processes = Process.GetProcessesByName(processName);

    foreach (Process process in processes)
    {
        if (process.MainModule.FileName == processName)
        {
            process.Kill();
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值