InstallFinalize 后 WIX 自定义操作修改 INSTALLFOLDER 中的文件 [英] WIX Custom Action modify file in INSTALLFOLDER after InstallFinalize

查看:63
本文介绍了InstallFinalize 后 WIX 自定义操作修改 INSTALLFOLDER 中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的 WIX V3 安装程序编写了一个 C# 自定义操作,它应该修改 INSTALLFOLDER 中的 appsettings.json.操作的 Execute-attribute 设置为immediate 和 Impersonate=no",它在 InstallFinalize 之后调用,但在此操作中遇到问题,即缺少管理员权限.该操作会修改 INSTALLFOLDER 中的 appsettings.json,即程序文件 (x86).

I have written a C# custom action for my WIX V3 Installer which is supposed to modify my appsettings.json in the INSTALLFOLDER. The action´s Execute-attribute is set to immediate and Impersonate="no",it is called after InstallFinalize but it encounters a problem within this action which is the missing admin permission. The action modifies appsettings.json in the INSTALLFOLDER which is Program File (x86).

自定义操作读取、反序列化、修改和序列化数据正常且没有错误.该错误发生在写入 InstallFolder 中的 appsettings.json 期间.尽管出现错误,但应用程序的其余部分已安装并且运行良好.

The custom action reads, deserializes, modifies, and serializes the data normally with no error. The error happens during writing to appsettings.json in InstallFolder. Although the error appears the rest of the application is installed and is working fine.

我尝试在 ALL 可能的组合中组合执行和自定义操作,虽然如果我在安装完成之前将自定义操作更改为运行,我可以获得写入 InstallFolder 的权限,但我找不到appsettings.json 文件,因为此时的所有文件都是临时文件 (.tmp),并且名称不相关.

I have tried combining Execute and Custom actions in ALL possible combinations, and while I get privileges to write to InstallFolder if I change the Custom Action to run before the installation is finished I can't find the appsettings.json file because all the files at that point are temporary files (.tmp), and with non-relevant names.

出现的错误:错误信息

我的 Product.wsx 代码的一部分:

Part of my Product.wsx code:

      <Property Id="Password" Value="Error password" />
      <Property Id="FilePath" Value="C:\Program Files (x86)\Company\Product\" />
      
      <CustomAction Id="SetUserName" Property="Username" Value="[ACCOUNT]"/>
      <CustomAction Id="SetPassword" Property="Password" Value="[PASSWORD]"/>
      <CustomAction Id="SetFilePath" Property="FilePath" Value="[INSTALLFOLDER]"/>
      
      <Binary Id="GetData" SourceFile="$(var.SetupExtensions.TargetDir)\$(var.SetupExtensions.TargetName).CA.dll" />  
      <CustomAction Id="ChangeJSON" BinaryKey="GetData" DllEntry="CustomAction1" Execute="immediate" Impersonate="no"  Return="check"/>
      
      <InstallExecuteSequence>
          <Custom Action="SetUserName" After="CostFinalize" />
          <Custom Action="SetPassword" After="CostFinalize" />
          <Custom Action="SetFilePath" After="CostFinalize"/>
          <Custom Action='ChangeJSON'  After='InstallFinalize'></Custom>
      </InstallExecuteSequence> 

我的自定义操作代码:

      public static ActionResult CustomAction1(Session session)
      {
          try
          {
              session.Log( "Begin CustomAction1" );
              string user = session["Username"];
              string password = session["Password"];
              string loc = session["FilePath"];

              var json = System.IO.File.ReadAllText( loc +"appsettings.json" );
              var root = JsonConvert.DeserializeObject<Root>(json);

              root.Default.UserName = user;
              root.Default.Password = password;
          
              json = JsonConvert.SerializeObject( root, Formatting.Indented );
              System.IO.File.WriteAllText( loc + "appsettings.json", json );
              //The MessageBox bellow shows(and is with correct info) when I remove System.IO.File.WriteAllText above ^^
              MessageBox.Show("Username: "+ user +"\nPassword: "+password +"\nFilePath: " + loc);
              return ActionResult.Success;
          }
          catch(Exception ex )
          {
              session.Log( "Error: " + ex.Message );
              MessageBox.Show(ex.Message);
              return ActionResult.Failure;
          }

如何通过我的自定义操作修改 appsettings.json?

推荐答案

应用程序启动:如果可以,我会考虑通过应用程序启动序列更新 JSON.1) 单一源代码解决方案2) 开发人员熟悉的领域3) 更简单、更好的调试功能4)no impersonation-sequencing-conditioning-complexities 就像自定义操作一样:为什么在我的 WiX/MSI 设置中限制自定义操作的使用是个好主意?

Application Launch: I would consider updating the JSON via the application launch sequence if you can. 1) Single source solution, 2) familiar territory for developers, 3) easier and better debugging features and 4) no impersonation-, sequencing- and conditioning-complexities like you get for custom actions: Why is it a good idea to limit the use of custom actions in my WiX / MSI setups?


延迟 CA 示例:您可以在此处找到延迟模式自定义操作 WiX 解决方案的示例:https://github.com/glytzhkof/WiXDeferredModeSample

内联示例:这个较旧的答案显示了答案中内联的关键结构:Wix 自定义操作 - 会话为空和错误推迟行动.

CustomActionData:延迟模式自定义操作无法像即时模式自定义操作那样访问会话对象属性值.正如 Painter 所写,您需要发送"通过将数据写入执行脚本,将文本或设置设置为延迟模式.然后通过读取特殊属性 CustomActionData 以延迟模式提供数据.上面的示例应该具有所需的构造以了解其工作原理.参见MSI 文档以及以及Robert Dickau 的 MSI 属性和延迟执行.

Transaction:延迟模式自定义操作只能存在于 InstallInitialize 和 InstallFinalize 之间.这些操作之间的操作以提升的权限运行,并且可以写入每台机器的位置(普通用户不可写).您可以在 InstallFinalize 之前为初学者安排自己的时间来测试您当前的删除机制(我会尝试其他方法).

Transaction: Deferred mode custom actions can only exist between InstallInitialize and InstallFinalize. Actions between these actions run with elevated rights and can write to per-machine locations (not writeable for normal users). You can schedule yourself right before InstallFinalize for starters to test your current delete mechanism (I would try other approaches).

回滚 CA:回滚自定义操作旨在撤消对 JSON 自定义操作所做的操作,然后将所有内容恢复到以前的状态(安装失败后进行清理).编写这些内容非常复杂,需要进行大量测试 - 许多人只是跳过它们.最好尝试找到适合您的库或框架.除了与下面链接的那个之外,我不知道其他任何东西,而且我不知道它的状态.回滚自定义操作必须在 InstallExecuteSequence 中的实际 CA 之前(当 MSI 回滚时,它会反向执行序列).

Rollback CAs: Rollback custom actions are intended to undo what was done with your JSON custom action and then revert everything to the previous state (cleanup after failed install). Writing these is quite involved and takes a lot of testing - many just skip them. It is best to try to find a library or a framework that does the job for you. I am not aware of any except the one linked to below, and I don't know its state. Rollback custom actions must precede the actual CA in the InstallExecuteSequence (when an MSI is rolling back it executes the sequence in reverse).

由于所有这些复杂性,自定义操作成为部署错误的主要来源:https://robmensching.com/blog/posts/2007/8/17/zataoca-custom-actions-are-generally-an-admission-of-失败/

With all this complexity, custom actions become the leading source of deployment errors: https://robmensching.com/blog/posts/2007/8/17/zataoca-custom-actions-are-generally-an-admission-of-failure/

链接:

  • How to pass CustomActionData to a CustomAction using WiX?
  • https://github.com/NerdyDuck/WixJsonExtension (not sure of status of this)

这篇关于InstallFinalize 后 WIX 自定义操作修改 INSTALLFOLDER 中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆