使用Winform C#,删除

时间:2017-01-29 15:05:48

标签: c# .net winforms

我有一个winform exe,它删除了一个应用程序和应用程序所需的文件夹。但我也想删除winform根文件夹。所以有一种方法可以这样做,因为我被告知它不可能删除其中运行的exe文件夹。那么是否有任何TEMP路径或类似的东西我可以复制卸载程序,因此它会删除设置根文件夹以及在完成该过程后自行删除。

由于

1 个答案:

答案 0 :(得分:4)

你可以运行一个不可见的CMD实例为你做删除:

ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", String.Format("/k {0} & {1} & {2}", "timeout /T 1 /NOBREAK >NUL", "rmdir /s /q \"" + Application.StartupPath + "\"", "exit"));
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

Process.Start(psi);

当您的应用程序即将关闭时执行上面的代码,它将删除它所在的目录。

使用的命令:

timeout /T x /NOBREAK >NUL
    - Wait for a certain amount of time.
        /T x     - Wait for x seconds.
        /NOBREAK - Specifies that it shouldn't be interrupted by pressing Space or Enter.
        >NUL     - Don't output any messages to the console.

rmdir /s /q <path>
    - Removes the directory <path>.
        /s - Remove subfiles and subdirectories.
        /q - Don't ask for confirmation.

exit
    - Closes the CMD instance

重要提示 真的 使用它时要小心,因为它会删除整个目录,无论其中包含什么内容!