gpt4 book ai didi

c# - WiX:在卸载时正确删除非空的临时文件和文件夹

转载 作者:太空狗 更新时间:2023-10-29 23:51:00 25 4
gpt4 key购买 nike

在我开始之前,让我注意到,我在这里的所有相关问题,等等,都没有找到解决办法。所以这个问题的目标和以前一样:
如何正确删除在应用程序使用wix工具工作时创建的临时文件夹和杂项文件?
到目前为止,我想出了以下方法:
使用customaction(例如,用c编写)只需删除所有文件和文件夹(这很好,但在我看来,此解决方案无效,因为不支持从msi侧回滚)。
使用util:removeFolderEx标记从WixUtilExtension。我做不到。
使用customaction(再次用c编写)在卸载之前填充msi db的表(目录和removefile)。这将迫使msi通过removefiles默认操作以其自己的方式正确卸载所有枚举的文件和文件夹(理论上它应该这样做)。
我把重点放在第三条道路上,在这里请你帮忙。
关于应用程序布局的几点注意事项
我构建的msi可以很好地处理程序文件、快捷方式、注册表和所有这些东西。但在安装过程中,我将一些配置文件放入C:\ProgramData\MyApp\文件夹(它们也被删除,没有任何问题)。
但是,当应用程序工作时,它会在C:\ProgramData\MyApp\中生成其他文件和目录,这些文件和目录用于在新版本可用时更新应用程序。假设用户在更新过程中关闭应用程序并希望卸载该应用程序。以下是我们目前在C:\ProgramData\MyApp文件夹中的内容:
C:\程序数据\MyApp\
C:\程序数据\MyApp\Temp\
C:\程序数据\myapp\temp\tool.exe
C:\ProgramData\MyApp\Temp\SomeLib.dll
C:\ProgramData\MyApp\Temp\ \SomeLiba.dll
C:\ProgramData\MyApp\Temp\ 在调整过程结束时,我想看到no C:\ProgramData\MyApp\文件夹。如果没有创建临时目录/文件,我可以看到这一点。
请注意,我不知道放入 C:\ProgramData\MyApp\Temp\文件夹的文件夹和文件的名称,因为最终的文件夹名称是使用guid自动生成的。
让我专注于项目中最重要的部分,并向您展示我迄今为止为完成任务所做的工作(请记住,我选择了第三种方式:通过customaction):
Main MyApp.wxs file

<Product Id=...>
...
<!-- Defines a DLL contains the RemoveUpdatesAction function -->
<Binary Id="RemoveUpdatesAction.CA.dll" src="RemoveUpdatesAction.CA.dll" />
<CustomAction Id="RemoveUpdatesAction"
Return="check"
Execute="immediate"
BinaryKey="RemoveUpdatesAction.CA.dll"
DllEntry="RemoveUpdatesAction" />
...
<InstallExecuteSequence>
<!-- Perform custom CleanUp only on 'UnInstall' action - see condition -->
<Custom Action='RemoveUpdatesAction' Before='RemoveFiles'>
(NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL")
</Custom>
</InstallExecuteSequence>
...
</Product>

在文件的后面,我为programdata目录定义了 Folder Layout
<Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
....
<!-- ...\Program Files\MyApp\ -->
<Directory Id="ProgramFilesFolder">
<Directory Id="APPINSTALLFOLDER" Name="MyApp" />
</Directory>

....

<!-- This is it! ...\ProgramData\MyApp\ -->
<Directory Id="CommonAppDataFolder">
<Directory Id="SETTINGSINSTALLFOLDER" Name="MyApp" />
</Directory>
....
</Directory>
</Fragment>

... a lot of different stuff here ...

<Fragment>
<Component Id="AddConfigurationFilesFolder" ...>
...
<!-- This component just serves as a bound point - see below -->
...
</Fragment>

到现在为止,一直都还不错。
现在, RemoveUpdatesAction.cs file包含自定义操作:
public class CustomActions
{
[CustomAction]
public static ActionResult RemoveUpdatesAction(Session session)
{
try
{
// Provide unique IDs
int indexFile = 1;
int indexDir = 1;

// Begin work
session.Log("Begin RemoveUpdatesAction");

// Bind to the component that for sure will be uninstalled during UnInstall action
// You can see this component mentioned above
const string componentId = "AddConfigurationFilesFolder";

// Get '..\{ProgramData}\MyApp' folder
// This property (SETTINGSINSTALLFOLDER) is mentioned too
string appDataFolder = session["SETTINGSINSTALLFOLDER"];

// Populate RemoveFile table in MSI database with all files
// created in '..\{ProgramData}\MyApp\*.*' folder - pls see notes at the beginning
if (!Directory.Exists(appDataFolder))
{
session.Log("End RemoveUpdatesAction");
return ActionResult.Success;
}

foreach (var directory in Directory.GetDirectories(appDataFolder, "*", SearchOption.AllDirectories))
{
session.Log("Processing Subdirectory {0}", directory);
foreach (var file in Directory.EnumerateFiles(directory))
{
session.Log("Processing file {0}", file);

string keyFile = string.Format("CLEANFILE_{0}", indexFile);

// Set values for columns in RemoveFile table:
// {1}: FileKey => just unique ID for the row
// {2}: Component_ => reference to a component existed in Component table
// In our case it is already mentioned 'AddConfigurationFilesFolder'
// {3}: FileName => localizable name of the file to be removed (with ext.)
// {4}: DirProperty => reference to a full dir path
// {5}: InstallMode => 3 means remove on Install/Remove stage
var fieldsForFiles = new object[] { keyFile, componentId, Path.GetFileName(file), directory, 3 };

// The following files will be processed:
// 1. '..\ProgramData\MyApp\Temp\tool.exe'
// 2. '..\ProgramData\MyApp\Temp\somelib.dll'
// 3. '..\ProgramData\MyApp\Temp\<UniqueFolderNameBasedOnGUID>\someliba.dll'
// 4. '..\ProgramData\MyApp\Temp\<UniqueFolderNameBasedOnGUID>\somelibb.dll'
InsertRecord(session, "RemoveFile", fieldsForFiles);

indexFile++;
}

string keyDir = string.Format("CLEANDIR_{0}", indexDir);

// Empty quotes mean we we want to delete the folder itself
var fieldsForDir = new object[] { keyDir, componentId, "", directory, 3 };

// The following paths will be processed:
// 1. '..\ProgramData\MyApp\Temp\'
// 2. '..\ProgramData\MyApp\Temp\<UniqueFolderNameBasedOnGUID>\'
InsertRecord(session, "RemoveFile", fieldsForDir);

indexDir++;
}

session.Log("End RemoveUpdatesAction");
return ActionResult.Success;
}
catch (Exception exception)
{
session.Log("RemoveUpdatesAction EXCEPTION:" + exception.Message);
return ActionResult.Failure;
}
}

// Took completely from another SO question, but is accoring to MSDN docs
private static void InsertRecord(Session session, string tableName, Object[] objects)
{
Database db = session.Database;
string sqlInsertSring = db.Tables[tableName].SqlInsertString + " TEMPORARY";
View view = db.OpenView(sqlInsertSring);
view.Execute(new Record(objects));
view.Close();
}
}

注意:卸载条件( (NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL"))是我从 read中获取的。
这几乎就是我所做的一切,在安装/卸载周期之后,我在msi日志文件和removefile表(通过枚举记录)中看到所有这些条目确实都被插入了。
但是无论我做什么, ..\ProgramData\MyApp\Temp\...中的文件和文件夹都会保留下来。
有人能澄清我做错了什么吗?
我认为问题可能在于如何在自定义操作类中定义dirproperty。我在那里放置了一个目录路径,但是我知道(使用orca)在directory表中没有我通过 Directory.GetDirectories找到的temp文件夹的记录。
所以removefile表中填充的记录对目录表的引用无效(对吗?)。我尝试手动将发现的文件夹添加到目录表中,但失败-每次尝试获取对目录表的引用时,都会出现异常。在msi中填充目录表的正确方法是什么?填充目录表有意义吗?
例如,如果我需要放置以下路径:
..\ProgramData\MyApp\Temp\
..\ProgramData\MyApp\Temp\<UniqueFolderNameBasedOnGUID>\
我该怎么做?
无论如何,请,建议任何东西-任何建议,提示或评论将不胜感激!
谢谢!
MSI卸载日志:
MSI (s) (B4:28) [05:28:39:427]: Doing action: RemoveUpdatesAction
....
MSI (s) (B4:4C) [05:28:39:450]: Invoking remote custom action. DLL: C:\WINDOWS\Installer\MSI7643.tmp, Entrypoint: RemoveUpdatesAction
....
Action start 5:28:39: RemoveUpdatesAction.
SFXCA: Extracting custom action to temporary directory: C:\WINDOWS\Installer\MSI7643.tmp-\
SFXCA: Binding to CLR version v4.0.30319
Calling custom action RemoveUpdatesAction!RemoveUpdatesAction.CustomActions.RemoveUpdatesAction
Begin RemoveUpdatesAction
Processing Subdirectory C:\ProgramData\MyApp\Temp
Processing file C:\ProgramData\MyApp\Temp\somelib.dll
Processing file C:\ProgramData\MyApp\Temp\tool.exe
Processing Subdirectory C:\ProgramData\MyApp\Temp\48574917-4351-4d4c-a36c-381f3ceb2e56
Processing file C:\ProgramData\MyApp\Temp\48574917-4351-4d4c-a36c-381f3ceb2e56\someliba.dll
Processing file C:\ProgramData\MyApp\Temp\48574917-4351-4d4c-a36c-381f3ceb2e56\somelibb.dll
End RemoveUpdatesAction
MSI (s) (B4:28) [05:28:39:602]: Doing action: RemoveFiles
MSI (s) (B4:28) [05:28:39:602]: Note: 1: 2205 2: 3: ActionText
Action ended 5:28:39: RemoveUpdatesAction. Return value 1.
Action start 5:28:39: RemoveFiles.
MSI (s) (B4:28) [05:28:39:607]: Note: 1: 2727 2: C:\ProgramData\MyApp\Temp
MSI (s) (B4:28) [05:28:39:607]: Note: 1: 2727 2: C:\ProgramData\MyApp\Temp\48574917-4351-4d4c-a36c-381f3ceb2e56
MSI (s) (B4:28) [05:28:39:607]: Counted 2 foreign folders to be removed.
MSI (s) (B4:28) [05:28:39:607]: Removing foreign folder: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Compass Mobile\
MSI (s) (B4:28) [05:28:39:607]: Removing foreign folder: C:\ProgramData\MyApp\
MSI (s) (B4:28) [05:28:39:607]: Doing action: RemoveFolders
MSI (s) (B4:28) [05:28:39:607]: Note: 1: 2205 2: 3: ActionText
Action ended 5:28:39: RemoveFiles. Return value 1.
Action start 5:28:39: RemoveFolders.

如你所见,我得到了2727个错误代码。 almost对于以下文件夹,目录表中没有记录:
C:\程序数据\MyApp\Temp
C:\ProgramData\MyApp\Temp\48574917-4351-4D4C-A36C-381F3CEB2E56
所以,也许我的建议是正确的?
提及的标记、msi表等的引用:
here
That means
Creating WiX Custom Actions in C# and Passing Parameters
MSI DB's RemoveFile table description
MSI DB's Directory table description

最佳答案

据我所知,您没有正确填充removefile表。如果您记录了确切的sql insert字符串来查看真正存在的内容,这将有所帮助。我认为您得到了错误2727,因为插入的第四个内容应该是引用msi文件中目录表的目录属性。它不是一个字面上的目录名——它应该是MSI文件目录表的一个键——据我所知,它是一个实际的目录,而不是目录表中的值。

关于c# - WiX:在卸载时正确删除非空的临时文件和文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com