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

gah*_*cep 5 .net c# windows-installer wix

在我开始之前,请注意,我在这里阅读了 几乎所有相关的问题,并且没有找到解决方案.所以问题的目标与之前的许多问题相同:

如何正确删除应用程序使用WiX工具时正在创建的临时文件夹和misc文件?

到目前为止,我想出了以下方法:

  1. 使用CustomAction(例如,在C#中编写)将简单地删除所有文件和文件夹(这很好用,但这种解决方法在我看来无效,因为不支持从MSI端回滚).

  2. 使用来自WixUtilExtension的util:RemoveFolderEx标记.我无法做到这一点.

  3. 使用CustomAction(再次在C#中编写)将在UnInstall发生之前填充MSI DB的表(DirectoryRemoveFile).这将迫使MSI正确地以自己的方式通过RemoveFiles默认操作卸载所有枚举的文件和文件夹(理论上它应该这样做).

我专注于第三种方式,并在这里向您提供一些帮助.

关于应用程序布局的一些注意事项 我建立的MSI与ProgramFiles,Shortcuts,Registry和所有这些东西配合得很好.但是在安装过程中,我将一些配置文件放入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\< UniqueFolderNameBasedOnGUID>\someliba.dll
C:\ ProgramData\MyApp\Temp\<UniqueFolderNameBasedOnGUID <\ somelibb.dll

在卸载过程结束时,我想看到没有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>
Run Code Online (Sandbox Code Playgroud)

稍后在文件中,我定义了Folder Layoutfor ProgramData目录:

<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>
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.

现在,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();
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:卸载条件((NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL"))我从这里开始.

这几乎就是我所做的一切,在安装/卸载周期之后,我在msi日志文件和RemoveFile表中看到(通过枚举记录)确实插入了所有这些条目. ..\ProgramData\MyApp\Temp\...无论我做什么,遗体中的文件和文件夹.

谁能澄清我做错了什么?

我相信问题可能出在我如何在Custom Action类中定义DirProperty.我在那里放了一个目录路径,但我知道(使用Orca)在Directory表中没有我发现的临时文件夹的记录Directory.GetDirectories.

所以RemoveFile表正在填充对Directory表有无效引用的记录(是吗?).我尝试手动将这些发现的文件夹添加到Directory表中但失败了 - 每次我尝试获取Directory表的引用时都会遇到异常.在MSI中填充目录表的正确方法是什么?根本填充目录表是否有意义?

例如,如果我需要放置以下路径:

  1. ..\ProgramData\MyApp\Temp\
  2. ..\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.  
Run Code Online (Sandbox Code Playgroud)

如您所见,我收到了2727错误代码.这意味着对于以下文件夹,目录表中没有记录:

C:\ ProgramData\MyApp\Temp
C:\ ProgramData\MyApp\Temp\48574917-4351-4d4c-a36c-381f3ceb2e56

那么,也许我的建议是正确的?

提到的标签,MSI表等的参考文献:

在C#中创建WiX自定义操作并传递参数
MSI DB的RemoveFile表说明
MSI DB的目录表说明
RemoveFiles操作
WiX CustomAction元素

Phi*_*lDW 0

据我所知,您没有正确填充RemoveFile 表。如果您记录准确的 SQL 插入字符串以查看其中的实际内容,将会有所帮助。我认为您收到错误 2727,因为插入中的第四件事应该是引用 MSI 文件中的目录表的目录属性。它实际上并不是一个目录名称 - 它应该是 MSI 文件的目录表的关键 - 据我从你的代码中可以看出,它是一个实际的目录,而不是目录表中的值。