• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

c#使用配置文件

武飞扬头像
故里2130
帮助1

在我们开发软件的时候,有时候有很多的配置文件,可以把配置的参数保存到本地,那么肯定要对文件进行读和写的操作,使用SharpConfig可以很简单的实现这个功能。

下面是GitHub的介绍。

https://codeload.github.com/cemdervis/SharpConfig/zip/refs/heads/master

1.使用.net6新建一个控制台程序

学新通

2. 在nuget中安装SharpConfig

学新通

3.在根目录中建立test.ini,其实别的后缀名文件也可以。

学新通 

文件内容

其中#开头的是注释内容,[]开头的目录,下面的具体值,就是key和value。

  1.  
    # 注释内容
  2.  
    [TEST1]
  3.  
    SomeInteger = 10
  4.  
    SomeFloat = 20.05
  5.  
    [TEST2]
  6.  
    Name = Peter
  7.  
    Age = 50
  8.  
    AnotherEmptyArray = { }

4. 代码如下

  1.  
    using SharpConfig;
  2.  
     
  3.  
    namespace ConsoleApp1
  4.  
    {
  5.  
    internal class Program
  6.  
    {
  7.  
    static void Main(string[] args)
  8.  
    {
  9.  
    var config = Configuration.LoadFromFile("test.ini"); //读取配置文件夹
  10.  
     
  11.  
    var section = config["TEST1"]; //读取根目录
  12.  
    var SomeInteger = section["SomeInteger"].StringValue; //读取具体值
  13.  
    //var SomeInteger = section["SomeInteger"].IntValue; //读取具体值
  14.  
    Console.WriteLine(SomeInteger);
  15.  
     
  16.  
    //section["SomeInteger"].SetValue("6666666666"); //修改值
  17.  
    section["SomeInteger"].StringValue="7777777777"; //修改值
  18.  
    config.SaveToFile("test.ini"); //保存值
  19.  
     
  20.  
     
  21.  
    Section test = new Section("TEST3"); //增加根目录
  22.  
    test.Add(new Setting("A", "123456")); //增加键值对
  23.  
    config.Add(test); //把键值对增加到文件
  24.  
    config.SaveToFile("test.ini"); //保存文件
  25.  
     
  26.  
     
  27.  
    Console.WriteLine("Hello, World!");
  28.  
    }
  29.  
    }
  30.  
    }
学新通

学新通  

每次修改值和增加值的时候,一定要保存文件,否则不会有效果。

而且设置值和修改值的时候, 有多种方式都可以做到,总体来说,还是很不错的,值得推荐使用。

对于官网的介绍其实更加的丰富,也可以直接存对象。

5.也可以直接创建ini文件

  1.  
    using SharpConfig;
  2.  
     
  3.  
    namespace ConsoleApp1
  4.  
    {
  5.  
    internal class Program
  6.  
    {
  7.  
    static void Main(string[] args)
  8.  
    {
  9.  
    //var config = Configuration.LoadFromFile("test.ini"); //读取配置文件夹
  10.  
     
  11.  
    //var section = config["TEST1"]; //读取根目录
  12.  
    //var SomeInteger = section["SomeInteger"].StringValue; //读取具体值
  13.  
    var SomeInteger = section["SomeInteger"].IntValue; //读取具体值
  14.  
    //Console.WriteLine(SomeInteger);
  15.  
     
  16.  
    section["SomeInteger"].SetValue("6666666666"); //修改值
  17.  
    //section["SomeInteger"].StringValue="7777777777"; //修改值
  18.  
    //config.SaveToFile("test.ini"); //保存值
  19.  
     
  20.  
     
  21.  
    //Section test = new Section("TEST3"); //增加根目录
  22.  
    //test.Add(new Setting("A", "123456")); //增加键值对
  23.  
    //config.Add(test); //把键值对增加到文件
  24.  
    //config.SaveToFile("test.ini"); //保存文件
  25.  
     
  26.  
     
  27.  
    //Console.WriteLine("Hello, World!");
  28.  
     
  29.  
    // Create the configuration.
  30.  
    var myConfig = new Configuration();
  31.  
     
  32.  
    // Set some values.
  33.  
    // This will automatically create the sections and settings.
  34.  
    myConfig["Video"]["Width"].IntValue = 1920;
  35.  
    myConfig["Video"]["Height"].IntValue = 1080;
  36.  
     
  37.  
    // Set an array value.
  38.  
    myConfig["Video"]["Formats"].StringValueArray = new[] { "RGB32", "RGBA32" };
  39.  
     
  40.  
    // Get the values just to test.
  41.  
    int width = myConfig["Video"]["Width"].IntValue;
  42.  
    int height = myConfig["Video"]["Height"].IntValue;
  43.  
    string[] formats = myConfig["Video"]["Formats"].StringValueArray;
  44.  
     
  45.  
     
  46.  
    myConfig.SaveToFile("1.ini");
  47.  
     
  48.  
     
  49.  
    }
  50.  
    }
  51.  
    }
学新通

6.效果,自动创建了1.ini

学新通 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghehhk
系列文章
更多 icon
同类精品
更多 icon
继续加载