在C#.NET应用程序中如何在卸载时删除文件夹? [英] How to delete Folder at uninstallation in C#.NET application?

查看:204
本文介绍了在C#.NET应用程序中如何在卸载时删除文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"XYZ"的窗口.net应用程序,我在安装后使用我的应用程序时创建了一个名为"ABC"的自定义文件夹(文件夹可能位于应用程序路径以外的任何位置).

I have a window .net application named "XYZ", I have created a custom folder named"ABC" (folder may be anywhere other than application path) while using my application after installation.

当我卸载应用程序时,所有文件夹均被删除,但"ABC"文件夹仍保留在那里.

When i am uninstalling the application all folders are removed but "ABC" folder remain there.

如何删除应用程序路径以外的'ABC'文件夹?

How can I delete 'ABC' folder which resides other than application path?

推荐答案

您必须为此使用自定义操作":

You have to use Custom Actions for that:

  1. 将新的库("CustomActions")添加到设置项目中
  2. 添加=>新项目=>安装程序类
  3. 切换到代码视图并覆盖Uninstall方法

代码:

public override void Uninstall(IDictionary savedState)
{
    base.Uninstall(savedState);

    // Delete folder here.
}

如果您不想编写自己的DeleteFolder方法,请添加对Microsoft.VisualBasic的引用:

If you don't want to write your own DeleteFolder method add a reference to Microsoft.VisualBasic:

 Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory("C:\\MyFiles", Microsoft.VisualBasic.FileIO.DeleteDirectoryOption.DeleteAllContents);

  1. 将CustomActions项目的项目输出(主要输出)添加到设置项目中.
  2. 右键单击您的安装项目,然后单击查看=>自定义操作
  3. 右键单击卸载=>添加自定义操作=>应用程序文件夹=> CustomActions的主要输出

注意:一个很好的例子位于这里.他们更详细地解释了此示例.最初在此答案中不明显的是,您必须在APPLICATION的项目中添加Installer类模板,而不是在Application的SETUP项目中添加模板.基本上,安装项目会从添加到安装项目中自定义操作"的任何应用程序中调用procs Install()和Uninstall().这个想法是要覆盖这两个过程以注入代码来进行出价...

Note: A great example of this is located here. They explain this example in greater detail. Something that was not obvious in this answer at first was the fact you had to add the Installer Class template within the APPLICATION'S project, NOT the Application's SETUP project. Basically the setup project calls the procs Install() and Uninstall() from any application that is added to the Custom Actions in the setup project. The idea is to override those two procs to inject code to do your bidding...

这篇关于在C#.NET应用程序中如何在卸载时删除文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆