【发布时间】:2021-03-29 04:23:19
【问题描述】:
我为我的 WIX V3 安装程序编写了一个 C# 自定义操作,它应该在 INSTALLFOLDER 中修改我的 appsettings.json。该操作的 Execute-attribute 设置为 immediate 和 Impersonate="no",它在 InstallFinalize 之后调用,但在此操作中遇到问题,即缺少管理员权限。 该操作会修改 INSTALLFOLDER 中的 appsettings.json,即 Program File (x86)。
自定义操作正常无错误地读取、反序列化、修改和序列化数据。 错误发生在写入 InstallFolder 中的 appsettings.json 期间。 虽然出现了错误,但应用程序的其余部分已安装并且工作正常。
我尝试在 ALL 可能的组合中组合执行和自定义操作,如果我在安装完成之前将自定义操作更改为运行,我将获得写入 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?
【问题讨论】:
-
看来您应该在自定义操作中设置
execute="deferred"。见:stackoverflow.com/a/24486554/2109769