C# WiX:卸载时正确删除非空临时文件和文件夹
在我开始之前,让我注意到,我在这里讨论了所有相关问题,但没有找到解决方案。因此,这个问题的目的与许多人以前的问题相同: 当应用程序使用WiX工具工作时,如何正确删除正在创建的临时文件夹和杂项文件 到目前为止,我想出了以下方法:C# WiX:卸载时正确删除非空临时文件和文件夹,c#,.net,wix,windows-installer,C#,.net,Wix,Windows Installer,在我开始之前,让我注意到,我在这里讨论了所有相关问题,但没有找到解决方案。因此,这个问题的目的与许多人以前的问题相同: 当应用程序使用WiX工具工作时,如何正确删除正在创建的临时文件夹和杂项文件 到目前为止,我想出了以下方法: 使用CustomAction(例如,用C#编写)可以简单地删除所有文件和文件夹(这非常有效,但在我看来,这种解决方法无效,因为不支持从MSI端回滚) 使用util:RemoveFolderEx标记从wixutileExtension移除。我做不到 在卸载之前,使用将填充M
C:\ProgramData\MyApp\
文件夹(它们也将被删除,没有任何问题)
但是,当应用程序运行时,它会在C:\ProgramData\MyApp\
中生成额外的文件和目录,用于在新版本可用时更新应用程序。让我们假设用户在更新过程中间关闭应用程序,并希望卸载应用程序。下面是我们目前在C:\ProgramData\MyApp
文件夹中的内容:
C:\ProgramData\MyApp\C:\ProgramData\MyApp\Temp\
C:\ProgramData\MyApp\Temp\tool.exe
C:\ProgramData\MyApp\Temp\somelib.dll
C:\ProgramData\MyApp\Temp\\someliba.dll
C:\ProgramData\MyApp\Temp\据我所知,您没有正确填充RemoveFile表。如果您记录了确切的SQL插入字符串以查看真正的内容,这会有所帮助。我认为您得到了错误2727,因为插入中的第四件事应该是一个目录属性,它引用MSI文件中的目录表。它不是一个真正的目录名——它应该是MSI文件目录表中的一个键——从代码中我可以看出,它是一个实际的目录,而不是目录表中的一个值 是的,你说得对。我确实在RemoveFile表中放入了一个真实的(不是目录标识符)。我也认为这是错误的,但是如果我不知道目录的名称,也不能更新目录表,那么如何为目录设置有效的ID(而不是路径)?我不会假设您需要更新目录表。我猜那里已经有一个目录,与要删除的文件的位置相关。或者它是一个标准的Windows文件夹,比如AppDataFolder,您可以添加目录表而不会引起任何问题。让我澄清一下。我在目录表中有
C:\ProgramData\MyApp
和C:\ProgramData\MyApp\Temp
项。但是如何为以下文件夹添加条目:C:\ProgramData\MyApp\Temp\6889D79E-EA63-42C8-A81D-7AF62A2B1E4F
?在新的更新过程开始之前,我不知道它的名称——它将只在运行时才被知道。稍后我需要删除该目录中的所有文件。我是否缺少smth?不,目录表中没有它们:)目录表中有一个引用它们的条目。插入字符串中的DirProperty必须引用该目录键。类似地,我假设AddConfigurationFilesFolder实际上是组件表中的一个组件条目。您的意思是,包含-\Temp
和\Temp\{GUID}
的顶级目录?是的,我有一个条目(SETTINGSINSTALLFOLDER
)。我试图为每个子目录指向该条目,但没有成功AddConfigurationFilesFolder
实际上是一个在SETTINGSINSTALLFOLDER
文件夹中定义的组件ID,它的条目位于组件表中。
<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>
<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>
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();
}
}
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.