How to delete Folder at uninstallation in C#.NET application?

岁酱吖の 提交于 2020-01-12 06:19:07

问题


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.

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

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


回答1:


You have to use Custom Actions for that:

  1. Add a new library ("CustomActions") to the setup project
  2. Add => New Item => Installer class
  3. Switch to code view and override the Uninstall method

Code:

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

    // Delete folder here.
}

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. Add the project output (Primary Output) of the CustomActions project to the setup project.
  2. Right click your setup project and click View => Custom Actions
  3. Right click uninstall => Add Custom Action => Application Folder => Primary Output of CustomActions

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...




回答2:


create custom installer, you can read here for more detailes EDIT Here is better link, the sample is in VB.NET, but is not difficult to understand ;)




回答3:


As per my knowledge, if you are using an installer to install the app & the folder is created by your app & not the installer, then it will not delete it.

Instead you will have to use a custom action that will run at un-installation which will delete it. Or create a custom C# app which is run only when uninstallation is in progress which will delete the folder. (I assume you are using some professional Installer like InstallShield)



来源:https://stackoverflow.com/questions/1117866/how-to-delete-folder-at-uninstallation-in-c-net-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!