编程语言
首页 > 编程语言> > C# 修改AppSetting.json并保存

C# 修改AppSetting.json并保存

作者:互联网

方法1:使用强类型

  1. 复制appsettings.Development.json内容,将json粘贴为类
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  }
}

  1. 生成代码如下
public class Rootobject
    {
        public Logging Logging { get; set; }
    }

    public class Logging
    {
        public Loglevel LogLevel { get; set; }
    }

    public class Loglevel
    {
        public string Default { get; set; }
        public string MicrosoftAspNetCore { get; set; }
    }
  1. 强类型读取
var config = new ConfigurationBuilder()
                .SetBasePath(_basePath)
                .AddJsonFile(_appsettingsProductionJson)
                .Build();
var o = config.Get<Rootobject>();
            

注意:这里有个坑,json文件中key是可以带英文.的;如文件中Microsoft.AspNetCore项
但是生成的类字段是没有.的如对应生成类字段 MicrosoftAspNetCore ,直接读取会读不出值

  1. 使用字串key方式
var config = new ConfigurationBuilder()
                .SetBasePath(_basePath)
                .AddJsonFile(_appsettingsProductionJson)
                .Build();
 var p = config["Logging:LogLevel:Microsoft.AspNetCore"];

  1. 修改配置文件
public bool Write<Rootobject>(Rootobject entity)
{
    config["Logging:LogLevel:Microsoft.AspNetCore"]="Information";
    var jsonString = File.ReadAllText(Path.Combine(_basePath,_appsettingsProductionJson), Encoding.UTF8);
    var jsonObject = JObject.Parse(jsonString);
    jsonObject["Logging"]["LogLevel"]["Microsoft.AspNetCore"] = config["Logging:LogLevel:Microsoft.AspNetCore"];
   
    var convertString = Convert.ToString(jsonObject);
    File.WriteAllText(Path.Combine(_basePath, _appsettingsProductionJson), convertString);
    return true;
}

标签:Logging,C#,AspNetCore,Microsoft,AppSetting,json,var,config,public
来源: https://www.cnblogs.com/wuhailong/p/16358393.html