Visual Studio安装项目-卸载时删除在运行时创建的文件 [英] Visual Studio Setup Project - Remove files created at runtime when uninstall
问题描述
我正在创建C#.NET WinForms应用程序,并且正在将安装程序创建为Visual Studio安装项目.
I am creating a C# .NET WinForms application and I am creating the installer as a Visual Studio setup project.
在Windows 10上,我可以在控制面板中删除已安装的文件.但是,在运行时,我的应用程序创建了一个包含日志文件的文件夹,并且在卸载应用程序时不会删除此文件夹和日志文件.
On Windows 10, I can remove the installed files in the control panel. However, during runtime my application creates a folder containing log files, and this folder and the log files are not removed when the app is uninstalled.
如何在卸载程序时也删除这些文件?
How can I make these files also be removed when the program is uninstalled?
推荐答案
您可以使用自定义安装程序操作在安装或卸载应用程序期间执行自定义操作.为此,您需要添加一个新的类库,其中包含一个从 CustomAction
.
You can use a custom installer action to perform custom actions during installation or uninstalling the application. To do so, you need to add a new class library containing a class which derives from CustomAction
.
要这样做,请按照下列步骤操作:
To do so, follow these steps:
- Add a new Setup project. (If you don't have project template, download and install it from here for VS2013, VS2015 or VS2017 and VS2019)
- Add primary output from your main project to the setup project.
- Add a new class library project.
- Add a new installer action to the class library project and use the code at the end of these steps.
- Add primary output of the class library to the setup project
- Right click on setup project in solution explorer and in view menu, select Custom Actions.
- In the custom actin editor, right click on uninstall and select Add Custom Action ... and select primary output of class library.
- Rename the action to RemoveFiles and in properties set
CustomActionData
property exactly to/path="[TARGETDIR]\"
. - Rebuild the solution and the setup project.
- Install the project.
自定义操作代码
添加对System.Configuration.Install
程序集的引用,然后将一个类添加到具有以下内容的项目.您可以在这里简单地拥有所需的任何逻辑.
Add a reference to System.Configuration.Install
assembly and then add a class to the project having following content. You can simply have any logic that you need here.
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
namespace InstallerActions
{
[RunInstaller(true)]
public partial class RemoveFiles : Installer
{
protected override void OnAfterUninstall(IDictionary savedState)
{
var path = System.IO.Path.Combine(Context.Parameters["path"], "log");
System.IO.Directory.Delete(path, true);
}
}
}
这篇关于Visual Studio安装项目-卸载时删除在运行时创建的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!