删除程序自身

 /// <summary>
        /// 删除程序自身
        /// </summary>
        private static void DeleteItself()
        {
            string vBatFile = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\DeleteItself.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", System.Windows.Forms.Application.ExecutablePath));
            }

            //************ 执行批处理
            WinExec(vBatFile, 0);
            
            //************ 结束退出
           // System.Windows.Forms.Application.Exit();

            System.Environment.Exit(0);
        }

[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern uint WinExec(string lpCmdLine, uint uCmdShow);

 

 

posted on 2019-05-30 09:30  菜鸟也想上天  阅读(188)  评论(0编辑  收藏  举报