思路一

通过调用 cmd 运行删除文件、目录命令实现。

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Dictionary<string,string> Uninstall_RemoveList = new Dictionary<string, string>() {
{ "d:\\test\\UninstallApp\\conf", "d" },
{ "d:\\test\\Uninstall\\UninstallApp.exe", "f" },
{ "d:\\test\\Uninstall\\UninstallApp.dll", "f" },
{ "d:\\test\\UninstallApp\\log", "d" }
};

//因最后程序退出会杀掉Process.Start()的实例,所以调用start再启动新的cmd实例
string delCmd = "start cmd /c \"timeout /t 1 /NOBREAK";
foreach (var item in Uninstall_RemoveList)
{
if (item.Value == "f" && File.Exists(item.Key))
delCmd += " && del /s /q \"" + item.Key + "\"";
else if (Directory.Exists(item.Key))
delCmd += " && rd /s /q \"" + item.Key + "\"";
}
delCmd += $" && mshta vbscript:msgbox(\"“{ProdConfig.Reg_Val_App_DisplayName}”卸载成功!\",64,\"“卸载成功”\")(window.close)\"";

var processStartInfo = new ProcessStartInfo()
{
Verb = "runas", // 管理员方式启动
FileName = "cmd.exe",
UseShellExecute = false,
CreateNoWindow = false,
Arguments = delCmd
};
Process.Start(processStartInfo);
Environment.Exit(0);

命令说明:

  • start cmd /c 启动一个新的 cmd.exe,执行完命令后退出(/k - 不退出);start | Microsoft Learn
  • timeout /t 1 /NOBREAK 等待1秒钟再继续执行(/NOBREAK - 忽略任何击键);cmd | Microsoft Learn
  • del /s /q 删除一个或多个文件。(/s - 从当前目录和所有子目录中删除指定的文件, 在文件被删除时显示文件的名称;/q - 指定静默模式,系统不会提示你进行删除确认);del | Microsoft Learn
  • rd /s /q 删除目录。参数同 delrd | Microsoft Learn

思路二

写入一个bat文件,执行删除命令。
令。