我为 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/