C#程序删除自身

 通过动态创建批处理文件
把程序运行的当前目录下的所有文件全部删除

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

此代码参考了

http://topic.csdn.net/u/20070619/12/e014238b-6eb7-4fb7-a7bb-0bca8d734384.html?seed=139148195&r=55739441#r_55739441

原文地址:https://www.cnblogs.com/liulun/p/1610717.html