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