gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:24:05 25 4
gpt4 key购买 nike

我已经为我的 WIX V3 安装程序编写了一个 C# 自定义操作,它应该是 在 INSTALLFOLDER 中修改我的 appsettings.json .操作的 Execute-attribute 设置为immediate 和 Impersonate="no",它在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com