自杀程序,删掉自身,包括执行程序所在的文件夹。
仅在windows下进行了测试,使用语言C#
string fileName = Path.GetTempPath() + "killmyself.bat";
FileInfo fi = new FileInfo(fileName);
if(fi.Exists)
fi.Delete();
StreamWriter bat = new StreamWriter(fi.OpenWrite());
bat.WriteLine("cd ..");
//bat.WriteLine($"del \"{Environment.CurrentDirectory}\" /q");
bat.WriteLine($"rd \"{Environment.CurrentDirectory}\" /Q /S"); //删除包括执行程序的文件夹
bat.WriteLine($"del \"{fileName}\" /q"); //删除临时bat文件
bat.Close();
ProcessStartInfo info = new ProcessStartInfo(fileName);
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
Environment.Exit(0);
递归删除目录的样例代码
/// <summary>
/// 递归删除目录
/// </summary>
/// <param name="dir"></param>
public static void Deletedir(DirectoryInfo dir)
{
if (!dir.Exists)
{
Console.WriteLine("Directory not exists");
return;
}
foreach (var item in dir.GetDirectories())
{
Deletedir(item);
}
try
{
dir.Delete(true);
Console.WriteLine("delete " + dir.FullName);
}
catch (Exception)
{
}
}