Created
April 4, 2022 02:00
-
-
Save fredgdaley2/129d4a4b236d9c40e05acdc340a08021 to your computer and use it in GitHub Desktop.
Compare appsettings.json files .Net 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Configuration; | |
var originalAppSettingsPath = "appsettings.json"; | |
var newAppsettingsPath = "newappsettings.json"; | |
var builder = new ConfigurationBuilder().AddJsonFile(originalAppSettingsPath).Build(); | |
var configElements = builder.GetChildren(); | |
var cnfgPaths = new List<ConfigPaths>(); | |
GetChildren(configElements, cnfgPaths); | |
var otherBuilder = new ConfigurationBuilder().AddJsonFile(newAppsettingsPath).Build(); | |
var newConfigElements = otherBuilder.GetChildren(); | |
var newCnfgPaths = new List<ConfigPaths>(); | |
GetChildren(newConfigElements, newCnfgPaths); | |
var onlyNewPaths = newCnfgPaths.ExceptBy(cnfgPaths.Select(cnf => cnf.Path), newcfg => newcfg.Path); | |
var updatedAndNew = newCnfgPaths.ExceptBy(cnfgPaths.Select(cnf => $"{cnf.Path}:{cnf.Value}"), newcfg => $"{newcfg.Path}:{newcfg.Value}"); | |
var diffValues = updatedAndNew.ExceptBy(onlyNewPaths.Select(npth => npth.Path), updated => updated.Path); | |
Console.WriteLine("Updated AppSettings"); | |
diffValues.ToList().ForEach(diff => Console.WriteLine($"{diff.Path} => {diff.Value}")); | |
Console.WriteLine(String.Empty.PadRight(20, '*')); | |
Console.WriteLine("New AppSettings"); | |
onlyNewPaths.ToList().ForEach(npth => Console.WriteLine($"{npth.Path} => {npth.Value}")); | |
Console.ReadLine(); | |
static void GetChildren(IEnumerable<IConfigurationSection> sections, List<ConfigPaths> cnfgPaths) | |
{ | |
foreach (var child in sections) | |
{ | |
if (child.Value != null) | |
{ | |
//Console.WriteLine($"{child.Path} => {child.Key} = {child.Value}"); | |
cnfgPaths.Add(new ConfigPaths { Path = child.Path, Value = child.Value }); | |
} | |
GetChildren(child.GetChildren(), cnfgPaths); | |
} | |
} | |
internal class ConfigPaths | |
{ | |
public string Path { get; set; } | |
public string Value { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment