C#程序删除自身
通过动态创建批处理文件
把程序运行的当前目录下的所有文件全部删除
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace killself
{
class Program
{
static void Main(string[] args)
{
BeginKillSelf();
}
[DllImport("kernel32.dll")]
public static extern uint WinExec(string lpCmdLine, uint uCmdShow);
private static void BeginKillSelf()
{
string vBatFile = Path.GetDirectoryName(Application.ExecutablePath) + "\\a.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);
}
}
}
此代码参考了