visual studio installer 卸载时不能删除安装目录问题

 

在vs中可以制作安装程序,但是这个安装程序默认卸载的时候不会把安装目录卸载,如果想在卸载的时候删除这个目录,那就要费点周折了。此方法同时适应于程序自删除以及工作目录删除。

基本思路是在程序要退出的时候启动一个脚本,该脚本会做好善后工作,删除所有不必要的东西。注意这个脚本应该是在程序完全退出以后再去删工作目录。

直接贴代码了,思路也很简单,主要是通过一个ping来进行延时,保证程序退出以后再删。

protected override void OnAfterUninstall(IDictionary savedState)

{

Assembly asm = Assembly.GetExecutingAssembly();

string path = asm.Location.Remove(asm.Location.LastIndexOf("\"));

DeleteAllSelf(path);

static void DeleteAllSelf(string path)

{

System.IO.Directory.SetCurrentDirectory("c:\");

string fileName = Path.Combine("c:\", "remove.bat");

StreamWriter bat = new StreamWriter(fileName, false, Encoding.Default);

bat.WriteLine("ping 127.0.0.1 -n 7 > nul"); //此处的延时非常必要,,保证删目录的时候程序已经完全退出了!

bat.WriteLine(string.Format("rd "{0}" /q /s", path));

bat.WriteLine(string.Format("del "{0}" /q", fileName));

 

bat.Close();

ProcessStartInfo info = new ProcessStartInfo(fileName);

info.WorkingDirectory = "c:\";

info.WindowStyle = ProcessWindowStyle.Hidden;

Process.Start(info);

}

原文地址:https://www.cnblogs.com/baizx/p/5342952.html