C#程序删除自身

时间:2021-02-05 23:07:43

最近自己写了个卸载程序,将程序本身放到安装目录,执行卸载程序的时候将安装目录删除。卸载程序本身无法删除自身。遂尝试通过批处理删除

            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "remove.bat");
            StreamWriter bat = new StreamWriter(fileName, false, Encoding.Default);
            bat.WriteLine(string.Format("del \"{0}\" /q", Application.ExecutablePath));
            bat.WriteLine(string.Format("del \"{0}\" /q",fileName));

            bat.Close();
            ProcessStartInfo info = new ProcessStartInfo(fileName);
            info.WindowStyle = ProcessWindowStyle.Hidden;
            Process.Start(info);
            Environment.Exit(0);