c# - InstallFinalize 后 WIX 自定义操作修改 INSTALLFOLDER 中的文件

标签 c# wix wix3.5 wix3 wix3.6

我为 WIX V3 安装程序编写了一个 C# 自定义操作,该操作应该修改 INSTALLFOLDER 中的 appsettings.json。该操作的执行属性设置为立即且模拟=“否”,它在InstallFinalize之后调用,但在此操作中遇到了一个问题,即缺少管理员权限。 该操作修改 INSTALLFOLDER 中的 appsettings.json,即程序文件 (x86)。

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

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

出现的错误: Error message

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

      <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?

最佳答案

更改系统状态的自定义操作应在 InstallIntialize 和 InstallFinalize 之间运行。这意味着您应该将其安排在 InstallFinalize 之前而不是之后。

它还应该以延迟执行的方式运行,而不进行模拟。您需要在此之前安排另一项自定义操作,以创建自定义操作数据并将数据传递到延迟的自定义操作。

理想情况下,您还应该具有回滚和提交操作来支持回滚并使用 WIXFAILWHENDEFERRED 自定义操作进行测试。

阅读: http://www.installsite.org/pages/en/isnews/200108/index.htm http://blog.iswix.com/2011/10/beam-me-up-using-json-to-serialize.html

关于c# - InstallFinalize 后 WIX 自定义操作修改 INSTALLFOLDER 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65358312/

相关文章:

Wix 包含文件 (.wxi) 引发异常

c# - 编译器提示缺少命名空间,该命名空间是类的名称

c# - 带 IDownloadManager 的 Windows 窗体 Webbrowswer 控件

c# - 允许使用 ASP.NET MVC 路由的字符串 ID

c# - 在 C# 字符串对象之间共享字符缓冲区

wix - 使用 WiX,如何根据安装时可用的内容将单个文件安装到(可能)多个子目录?

WiX Bootstrap 主题文件?

c# - 安装文件或程序在安装时安装在用户定义的路径

wix - TeamCity 的 AssemblyInfo 修补程序的数字格式

wix - 使用在 Wix 中创建的其他 x86 mai 包写入注册表的 x64 部分