c# - 使用 'node:child' 表示法搜索 json 属性的最佳方法
问题描述
在基于 MSI 的安装过程中,我们有一个过程,即我们通过参数和/或安装程序表单获取 appsetting 值。
我正在尝试动态更新应用程序 appsettings 文件的 json 属性,但不确定如何使用“node:child:child”的密钥格式在任何深度搜索 json。所有选项都在桌面上,我们能够序列化对象并使用反射、linq 解决方案或其他任何东西。这将非常类似于 json 替换在 Azure 构建或发布管道中的工作方式。
目前有这个非常接近,除了我无法从 IConfiguration 中提取 json 字符串以便将其写回文件.....
public static ActionResult ModifySettings(Session session)
{
session.Log("Begin ModifySettings");
try
{
string[] customData = session.CustomActionData.Keys.ToArray();
string[] customValues = session.CustomActionData.Values.ToArray();
///Product.wxs -> BinDir=[BinDir]
var installDirectory = customValues[1];
string file = Path.Combine(installDirectory, "appsettings.json");
session.Log(string.Format($"Updating file: {file}.{Environment.NewLine}."));
if (File.Exists(file))
{
session.Log($"Updating Settings file: {file}.{Environment.NewLine}");
var configFile = new ConfigurationBuilder().AddJsonFile(file).Build();
for (int i = 3; i < customData.Count(); i++)
{
if (!ReplaceSetting(configFile, customData[i], customValues[i], session))
throw new ArgumentException($"Error during settings replacement of {customData[i]} with {customValues[i]}",customData[i]);
}
var settingsAsString = configFile.ToString();//DOES NOT WORK
session.Log($"Updated settings{Environment.NewLine}{settingsAsString}{Environment.NewLine}");
File.WriteAllText(file, settingsAsString);
}
else
{
session.Log($"Failed to update file {file}.{Environment.NewLine}Exiting.");
return ActionResult.Failure;
}
return ActionResult.Success;
}catch(Exception ex)
{
session.Log(ex.Message);
return ActionResult.Failure;
}
}
private static bool ReplaceSetting(IConfigurationRoot settings, string settingName, string settingValue, Session session)
{
try
{
session.Log(string.Format($"Updating Setting: {settingName}{Environment.NewLine}Value: {settingValue}{Environment.NewLine}"));
settings[settingName] = settingValue; //<- THIS IS THE PART I NEED TO MIMIC WHEN SETTINGNAME FORMAT IS SOMETHING:SOMETHINGELSE:PROPERTY!!!
}
catch(Exception ex)
{
session.Log(ex.Message);
return false;
}
return true;
}
使用 JObject 编辑工作解决方案
public static ActionResult ModifySettings(Session session)
{
session.Log("Begin ModifySettings");
try
{
string[] customData = session.CustomActionData.Keys.ToArray();
string[] customValues = session.CustomActionData.Values.ToArray();
///Product.wxs -> BinDir=[BinDir]
var installDirectory = customValues[1];
string file = Path.Combine(installDirectory, "appsettings.json");
session.Log(string.Format($"Updating file: {file}.{Environment.NewLine}."));
if (File.Exists(file))
{
session.Log($"Updating Settings file: {file}.{Environment.NewLine}");
var configFile = new ConfigurationBuilder().AddJsonFile(file).Build();
JObject settings = JObject.Parse(File.ReadAllText(file));
for (int i = 3; i < customData.Count(); i++)
{
if (!ReplaceSetting(ref settings, customData[i], customValues[i], session))
throw new ArgumentException($"Error during settings replacement of {customData[i]} with {customValues[i]}",customData[i]);
}
session.Log($"Updated settings{Environment.NewLine}{settings.ToString()}{Environment.NewLine}");
File.WriteAllText(file, settings.ToString());
}
else
{
session.Log($"Failed to update file {file}.{Environment.NewLine}Exiting.");
return ActionResult.Failure;
}
return ActionResult.Success;
}catch(Exception ex)
{
session.Log(ex.Message);
return ActionResult.Failure;
}
}
private static bool ReplaceSetting(ref JObject settingFile, string settingName, string settingValue, Session session)
{
try
{
session.Log(string.Format($"Updating Setting: {settingName}{Environment.NewLine}Value: {settingValue}{Environment.NewLine}"));
var token = settingFile.SelectToken(settingName);
(token.Parent as JProperty).Value = settingValue;
}
catch (Exception ex)
{
session.Log(ex.Message);
return false;
}
return true;
}
解决方案
推荐阅读
- python - 如何将两个不同的元组列表取消列出到相同的结构并附加到文件对象?
- gatsby - 创建 gatsby 源插件时遇到问题
- bootstrap-modal - 引导向导在引导模式或 jquery-ui 对话框中不起作用
- ios - 相同的图像切片在 8 和 8 PLUS 上显示非常不同
- html - 如何在单选按钮之间画一条线?
- excel - 特定评论的搜索范围
- reactjs - 使用外部样式表在 React 中测量组件大小不正确
- ajax - 如何在 django 的表单中添加下拉列表?
- python - 为什么 FastText 词嵌入可以从另一种语言生成词的表示?
- laravel - 将默认 Laravel 身份验证电子邮件字段更改为 user_email