在我们开发软件的时候,有时候有很多的配置文件,可以把配置的参数保存到本地,那么肯定要对文件进行读和写的操作,使用SharpConfig可以很简单的实现这个功能。
下面是GitHub的介绍。
https://codeload.github.com/cemdervis/SharpConfig/zip/refs/heads/master
1.使用.net6新建一个控制台程序

2. 在nuget中安装SharpConfig

3.在根目录中建立test.ini,其实别的后缀名文件也可以。
 
 
文件内容
其中#开头的是注释内容,[]开头的目录,下面的具体值,就是key和value。
  
   - 
    
     
    
    
     
      # 注释内容
     
    
- 
    
     
    
    
     
      [TEST1]
     
    
- 
    
     
    
    
     
      SomeInteger = 10
     
    
- 
    
     
    
    
     
      SomeFloat = 20.05
     
    
- 
    
     
    
    
     
      [TEST2]
     
    
- 
    
     
    
    
     
      Name = Peter
     
    
- 
    
     
    
    
     
      Age = 50
     
    
- 
    
     
    
    
     
      AnotherEmptyArray = {      }
     
    
4. 代码如下
  
   - 
    
     
    
    
     
      using SharpConfig;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      namespace 
      ConsoleApp1
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
         
      internal 
      class 
      Program
     
    
- 
    
     
    
    
     
          {
     
    
- 
    
     
    
    
             
      static void Main(string[] args)
     
    
- 
    
     
    
    
     
              {
     
    
- 
    
     
    
    
                 
      var config = Configuration.LoadFromFile(
      "test.ini");    
      //读取配置文件夹
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
                 
      var section = config[
      "TEST1"];         
      //读取根目录
     
    
- 
    
     
    
    
                 
      var SomeInteger = section[
      "SomeInteger"].StringValue;   
      //读取具体值
     
    
- 
    
     
    
    
                 
      //var SomeInteger = section["SomeInteger"].IntValue; //读取具体值
     
    
- 
    
     
    
    
     
                  Console.WriteLine(SomeInteger);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
                 
      //section["SomeInteger"].SetValue("6666666666"); //修改值
     
    
- 
    
     
    
    
     
                  section[
      "SomeInteger"].StringValue=
      "7777777777";       
      //修改值
     
    
- 
    
     
    
    
     
                  config.SaveToFile(
      "test.ini");                       
      //保存值
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
                  Section test = 
      new Section(
      "TEST3");            
      //增加根目录
     
    
- 
    
     
    
    
     
                  test.Add(
      new Setting(
      "A", 
      "123456"));           
      //增加键值对
     
    
- 
    
     
    
    
     
                  config.Add(test);                               
      //把键值对增加到文件
     
    
- 
    
     
    
    
     
                  config.SaveToFile(
      "test.ini");                  
      //保存文件
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
                  Console.WriteLine(
      "Hello, World!");
     
    
- 
    
     
    
    
     
              }
     
    
- 
    
     
    
    
     
          }
     
    
- 
    
     
    
    
     
      }
     
    
  
  
每次修改值和增加值的时候,一定要保存文件,否则不会有效果。
而且设置值和修改值的时候, 有多种方式都可以做到,总体来说,还是很不错的,值得推荐使用。
对于官网的介绍其实更加的丰富,也可以直接存对象。
5.也可以直接创建ini文件
  
   - 
    
     
    
    
     
      using SharpConfig;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      namespace 
      ConsoleApp1
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
         
      internal 
      class 
      Program
     
    
- 
    
     
    
    
     
          {
     
    
- 
    
     
    
    
             
      static void Main(string[] args)
     
    
- 
    
     
    
    
     
              {
     
    
- 
    
     
    
    
                 
      //var config = Configuration.LoadFromFile("test.ini"); //读取配置文件夹
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
                 
      //var section = config["TEST1"]; //读取根目录
     
    
- 
    
     
    
    
                 
      //var SomeInteger = section["SomeInteger"].StringValue; //读取具体值
     
    
- 
    
     
    
    
                 
      var SomeInteger = section[
      "SomeInteger"].IntValue;   
      //读取具体值
     
    
- 
    
     
    
    
                 
      //Console.WriteLine(SomeInteger);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
                  section[
      "SomeInteger"].SetValue(
      "6666666666");       
      //修改值
     
    
- 
    
     
    
    
                 
      //section["SomeInteger"].StringValue="7777777777"; //修改值
     
    
- 
    
     
    
    
                 
      //config.SaveToFile("test.ini"); //保存值
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
                 
      //Section test = new Section("TEST3"); //增加根目录
     
    
- 
    
     
    
    
                 
      //test.Add(new Setting("A", "123456")); //增加键值对
     
    
- 
    
     
    
    
                 
      //config.Add(test); //把键值对增加到文件
     
    
- 
    
     
    
    
                 
      //config.SaveToFile("test.ini"); //保存文件
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
                 
      //Console.WriteLine("Hello, World!");
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
                 
      // Create the configuration.
     
    
- 
    
     
    
    
                 
      var myConfig = 
      new Configuration();
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
                 
      // Set some values.
     
    
- 
    
     
    
    
                 
      // This will automatically create the sections and settings.
     
    
- 
    
     
    
    
     
                  myConfig[
      "Video"][
      "Width"].IntValue = 
      1920;
     
    
- 
    
     
    
    
     
                  myConfig[
      "Video"][
      "Height"].IntValue = 
      1080;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
                 
      // Set an array value.
     
    
- 
    
     
    
    
     
                  myConfig[
      "Video"][
      "Formats"].StringValueArray = 
      new[] { 
      "RGB32", 
      "RGBA32" };
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
                 
      // Get the values just to test.
     
    
- 
    
     
    
    
                 
      int width = myConfig[
      "Video"][
      "Width"].IntValue;
     
    
- 
    
     
    
    
                 
      int height = myConfig[
      "Video"][
      "Height"].IntValue;
     
    
- 
    
     
    
    
                 
      string[] formats = myConfig[
      "Video"][
      "Formats"].StringValueArray;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
                  myConfig.SaveToFile(
      "1.ini");
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
              }
     
    
- 
    
     
    
    
     
          }
     
    
- 
    
     
    
    
     
      }
     
    
 6.效果,自动创建了1.ini
 
 
转载:https://blog.csdn.net/u012563853/article/details/128639712
查看评论
					 
					