//利用批处理文件实现

[DllImport("kernel32.dll")]
public static extern uint WinExec(string lpCmdLine, uint uCmdShow);
private void button1_Click(object sender, EventArgs e)


{
string vBatFile = Path.GetDirectoryName(Application.ExecutablePath) + "\\Zswang.bat";
using (StreamWriter vStreamWriter = new StreamWriter(vBatFile, false, Encoding.Default))

{
vStreamWriter.Write(string.Format(
":del\r\n" +

" del \"
{0}\"\r\n" +

"if exist \"
{0}\" goto del\r\n" + //此处已修改
"del %0\r\n", Application.ExecutablePath));
}
WinExec(vBatFile, 0);
Close();
}

本文介绍了一种使用C#调用批处理文件实现程序自我删除的方法。通过创建一个批处理文件并利用WinExec函数执行,该程序能够在运行结束后自行删除,避免了残留文件的问题。

413

被折叠的 条评论
为什么被折叠?



