Skip to content

Commit

Permalink
Fixed appSetting.json not updating because of path issue when startin…
Browse files Browse the repository at this point in the history
…g from another location

Path.GetCurrentDirectory() was misunderstood.
changed to Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
  • Loading branch information
DineshSolanki committed Sep 3, 2020
1 parent 6893b60 commit f9062cb
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions BatchMuxer_SubEd_Console/classes/util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;

namespace BatchMuxer_SubEd_Console.Classes
{/// <summary>
Expand Down Expand Up @@ -105,20 +106,23 @@ public static bool RenameFile(FileInfo[] fi)

return hasRenamed;
}

/// <summary>
/// Update appsetting.json
/// </summary>
/// <param name="key">key to update</param>
/// <param name="value">new value</param>
public static void WriteToConfig(string key, string value)
{
string json = File.ReadAllText("appsettings.json");
string appSettingPath =
Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "appsettings.json");
string json = File.ReadAllText(appSettingPath);
var application = new Application();
JsonConvert.PopulateObject(json, application);
dynamic jsonObj = JsonConvert.DeserializeObject(json);
jsonObj["application"][key] = value;
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText("appsettings.json", output);
File.WriteAllText(appSettingPath, output);
}
}
}

0 comments on commit f9062cb

Please sign in to comment.