I have this appsetting json file :
"RavenOptions": {
"PublicUrl": "http://127.0.0.1:61570",
"PublicDbName": "TestHostBuilder_ctor_1",
"TseDbName": "TestHostBuilder_ctor_1",
"IsHttps": "false",
"CertificateDirectory": "",
"ShardUrls": [
"http://127.0.0.1:61570",
"http://127.0.0.1:61570",
"http://127.0.0.1:61570",
"http://127.0.0.1:61570"
]
}
I need to update the values of the file in runtime .I am using this function :
public static void AddOrUpdateAppSetting<T>(string key, T value)
{
try
{
var filePath = Path.Combine(AppContext.BaseDirectory, "appSettingTest.json");
string json = File.ReadAllText(filePath);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
var sectionPath = key.Split(":")[0];
if (!string.IsNullOrEmpty(sectionPath))
{
var keyPath = key.Split(":")[1];
jsonObj[sectionPath][keyPath] = value;
}
else
{
jsonObj[sectionPath] = value; // if no sectionpath just set the value
}
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(filePath, output);
}
catch (ConfigurationErrorsException)
{
Console.WriteLine("Error writing app settings");
}
}
this function works fine but the problem is when I want to update the values of sharedurls
(this is an array) the result is like this :
All records are in one row .But I need tho be like this :
every records is one row. Here is my update code :
string serverAddress = "\""+documentStore.Identifier.Split("(").First()+"\"";
Appset.AddOrUpdateAppSetting("RavenOptions:PublicDbName", documentStore.Database);
Appset.AddOrUpdateAppSetting("RavenOptions:PublicUrl", documentStore.Identifier.Split("(").First());
Appset.AddOrUpdateAppSetting("RavenOptions:ShardUrls","["+String.Join(",", Enumerable.Repeat(serverAddress, 4).ToArray())+"]");