////////////////update 2006.4.25 15:20 支持64kb以上的执行文件///////////////////
/////////////////////复制以上内容到文本文件内,并保存为bat 类型运行即可///////////////////////
//////以下为仅支持64kb以内的执行文件的转换代码///////////
记得1997年那个叫"OMNISCENT"的DEMO引起了很多人的兴趣,今天我也写了个,基于.net 2.0 环境的一个工具软件,叫做EXE2BAT,可以转换64KB以下的执行文件为批处理文本文件,只需要执行批处理即可运行程序。
以下是工具的执行代码,请复制到文本文件内,并修改名称为 exe2bat.bat ,并运行它。
运行界面为:

程序原始代码为:


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

namespace ExeToBatch


{
public partial class FormMain : Form

{
public FormMain()

{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}

Thread t = null;
private void btnLoad_Click(object sender, EventArgs e)

{
DialogResult dr = openFileDialog1.ShowDialog(this);
btnConvert.Enabled = dr == DialogResult.OK;
}

private void Convert(string fileName)

{
ThreadWithState tws = new ThreadWithState(fileName, new ProgressCallback(ResultCallback), new CompleteCallback(Complete));
t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
}

//回调
public void ResultCallback(int Max, int Min, int Position)

{
this.progressBar1.Maximum = Max;
this.progressBar1.Minimum = Min;
this.progressBar1.Value = Position;
}

public void Complete()

{
MessageBox.Show("任务完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnConvert.Enabled = false;
}

private void btnExit_Click(object sender, EventArgs e)

{
if (t != null)

{
t.Abort();
t.Join();
}
this.Dispose();
}

private void btnConvert_Click(object sender, EventArgs e)

{
Convert(openFileDialog1.FileName);
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

{
System.Diagnostics.Process.Start("explorer.exe", "http://chinasf.cnblogs.com");
System.Diagnostics.Process.Start("explorer.exe", "mailto:chinasf@hotmail.com");
}

}

public delegate void ProgressCallback(int Max, int Min, int Position);
public delegate void CompleteCallback();

public class ThreadWithState

{
private string fileName;
private ProgressCallback callback;
private CompleteCallback cmpback;
public ThreadWithState(string fileName, ProgressCallback callbackDelegate, CompleteCallback cmpbackDelegate)

{
this.fileName = fileName;
this.callback = callbackDelegate;
this.cmpback = cmpbackDelegate;
}

public void ThreadProc()

{
//处理文件
try

{
if (File.Exists(this.fileName))

{
FileStream fileReader = new FileStream(this.fileName, FileMode.Open, FileAccess.Read);
byte[] fileBytes = new byte[fileReader.Length];
fileReader.Read(fileBytes, 0, fileBytes.Length);
fileReader.Close();


int k = 256;
StringBuilder echo = new StringBuilder();
echo.AppendFormat("@echo 正在装入数据,请等待
\r\n");

int b = 0;
int cb = 0;
for (int i = 0; i < fileBytes.Length; i++)

{
b++;
if (b == 1)

{
echo.AppendFormat("@echo e{0} ", k.ToString("x4"));
}
echo.AppendFormat("{0} ", fileBytes[i].ToString("x"));
if (b >= 16)

{
b = 0;
echo.AppendFormat(">>Datares\r\n");

}
k++;

cb++;
if (callback != null && cb % 1000==0 )
callback(fileBytes.Length, 0, i);
}

if (b < 16 && b!=0)
echo.AppendFormat(">>Datares\r\n");

//echo.AppendFormat("@echo g >> Datares\r\n");

echo.AppendFormat("@echo 重建执行程序 {0} \r\n", new FileInfo(this.fileName).Name);
echo.AppendFormat("@echo rcx >> Datares\r\n");
echo.AppendFormat("@echo {0} >> Datares\r\n", fileBytes.Length.ToString("x"));
echo.AppendFormat("@echo n TmpExe >> Datares\r\n");
echo.AppendFormat("@echo w >> Datares\r\n");
echo.AppendFormat("@echo q >> Datares\r\n");
echo.AppendFormat("@debug < Datares > nul\r\n");
echo.AppendFormat("@del Datares > nul\r\n");
echo.AppendFormat("@ren TmpExe {0}> nul\r\n", new FileInfo(this.fileName).Name);
echo.AppendFormat("@echo 启动执行程序 {0} \r\n", new FileInfo(this.fileName).Name);
echo.AppendFormat("@{0}", new FileInfo(this.fileName).Name);

StreamWriter sw = new StreamWriter(new FileInfo(this.fileName).DirectoryName + "\\" + new FileInfo(this.fileName).Name + ".bat", false, Encoding.Default);
sw.Write(echo.ToString());
sw.Close();

if (callback != null)
callback(1, 0, 1);

if (cmpback != null)
cmpback();
}
}
catch (ThreadAbortException)

{
Thread.ResetAbort();
}
}
}

}
运行原理是通过DEBUG,把数据装入之后恢复为原始文件。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构