C# Wix CustomAction可以';t在安装前使用访问写入的文件Finalize

C# Wix CustomAction可以';t在安装前使用访问写入的文件Finalize,c#,wix,windows-installer,custom-action,C#,Wix,Windows Installer,Custom Action,我有一个Wix项目,它应该安装我的c#应用程序。文件复制和防火墙例外已在工作。现在我想用一个自定义操作修改json配置文件。我想读取所有json文件,更改一些值并将它们写回程序文件。以下是我的CA定义: <CustomAction Id="PrepareConfigurationFilesCustomAction" BinaryKey="InstallerCA" DllEntry="PrepareConfigurationFilesCustomAction" Execute="immedi

我有一个Wix项目,它应该安装我的c#应用程序。文件复制和防火墙例外已在工作。现在我想用一个自定义操作修改json配置文件。我想读取所有json文件,更改一些值并将它们写回程序文件。以下是我的CA定义:

<CustomAction Id="PrepareConfigurationFilesCustomAction" BinaryKey="InstallerCA" DllEntry="PrepareConfigurationFilesCustomAction" Execute="immediate" Impersonate="no" Return="check" />
<CustomAction Id="CreateConfigurationFiles" BinaryKey="InstallerCA" DllEntry="CreateConfigurationFiles" Execute="deferred" Impersonate="no" Return="check" />
<Custom Action="PrepareConfigurationFilesCustomAction" Before="CreateConfigurationFiles">NOT Installed AND NOT REMOVE</Custom>
<Custom Action="CreateConfigurationFiles" Before="InstallFinalize">NOT Installed AND NOT REMOVE</Custom>
但是当我运行.msi时,我得到了一个异常

Excetion TrownSystem.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\Vendor\Product\etc\Configurations'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileSystemEnumerableIterator`1.CommonInit()
   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
   at System.IO.Directory.GetFiles(String path)
   at Vendor.Application.Product.Setup.Actions.CustomActionData.ConfigurationFilesCustomActionData.CreateSessionCustomActionData(Session session) in C:\work\\Projects\Installation\Packages\Product.Workstation\Setup.Actions\CustomActionData\ConfigurationFilesCustomActionData.cs:line 99
当我附加调试器时,我可以在代码和文件系统中看到文件不存在

如果我将第一个Ca(PrepareConfigurationFilesCustomAction)更改为Execute=“deferred”,则会写入文件,但无法访问会话值

对我有什么想法或解决办法吗

谢谢

解决方案?:最好的解决方案是将JSON文件安装到只读位置,然后使用应用程序将其复制到用户配置文件中,并根据需要进行更新。这并不总是可能的——例如当没有要启动的应用程序二进制文件时

访问被拒绝:您不能作为普通用户写入程序文件下的位置。这里有一篇关于这个问题的文章:

技术性:出现这种行为的原因是InstallExecuteSequence被处理了两次。第一个是“即时模式”,它运行即时模式自定义操作,并为执行构建事务脚本。当您到达InstallFinalize时,序列将从InstallInitialize再次运行,此时系统将更新为文件安装、注册表更改等。。。此模式中的自定义操作将被延迟。他们不能直接访问属性。属性值必须通过称为CustomActionData的机制“发送”给它们。相当笨重。如果您使用该应用程序进行工作,则不需要

CustomActionData的概念之前已经被其他人很好地解释过了,因此我仅链接到您可以在其他地方访问的信息:

Excetion TrownSystem.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\Vendor\Product\etc\Configurations'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileSystemEnumerableIterator`1.CommonInit()
   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
   at System.IO.Directory.GetFiles(String path)
   at Vendor.Application.Product.Setup.Actions.CustomActionData.ConfigurationFilesCustomActionData.CreateSessionCustomActionData(Session session) in C:\work\\Projects\Installation\Packages\Product.Workstation\Setup.Actions\CustomActionData\ConfigurationFilesCustomActionData.cs:line 99