首页 > 编程语言 >C#之构建配置文件实现获取并保存功能

C#之构建配置文件实现获取并保存功能

时间:2024-07-16 19:29:17浏览次数:8  
标签:return string 配置文件 C# Section 构建 Key static NoText

实现工具类,会创建一个Config.ini的配置文件

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace ConfigUtil
{
    class ConfigUtils
    {
        #region API函数声明

        [DllImport("kernel32")]//返回0表示失败,非0为成功
        private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);

        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32")]
        public static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);


        #endregion

        #region 读Ini文件
        //Section参数、Key参数和IniFilePath不用再说,Value参数表明key的值,
        //而这里的NoText对应API函数的def参数,它的值由用户指定,是当在配置文件中没有找到具体的Value时,就用NoText的值来代替。
        //NoText 可以为null或""
        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }
        public static string ReadIni(string Section, string Key, string NoText)
        {
            return ReadIniData(Section, Key, NoText, ".\\Config\\Config.ini");
        }
        #endregion

        #region 写Ini文件

        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        public static bool WritrIni(string Section, string Key, string Value)
        {
            return WriteIniData(Section, Key, Value, ".\\Config\\Config.ini");
        }
        #endregion
    }
}

  

文件的内容样式如下

[TEST]
test=123

  

具体的实现,下列是读取的引用

string test = ConfigUtils.ReadIni("TEST", "test", "");

  

写入的方式如下

ConfigUtils.WritrIni("TEST", "test", "123");

  

 

标签:return,string,配置文件,C#,Section,构建,Key,static,NoText
From: https://www.cnblogs.com/wzhsun/p/18305964

相关文章

  • Windows图形界面(GUI)-DLG-C/C++ - 列表框(ListBox)
    公开视频-> 链接点击跳转公开课程博客首页-> ​​​​​​链接点击跳转博客主页列表框(ListBox)控件类型单选列表框(Single-selectionListBox):用户一次只能选择一个列表项。它通常用于当选择范围被限定到一个单一的选项时。多选列表框(Multi-selectionListBox):用户可以同......
  • Windows图形界面(GUI)-DLG-C/C++ - 静态控件(Static)
    公开视频-> 链接点击跳转公开课程博客首页-> ​​​​​​链接点击跳转博客主页目录静态控件(Static)控件样式消息处理实例代码静态控件(Static)控件样式文本(Text):用来显示文本信息。可以是简单的一行文本或者复杂的格式化文本。图标(Icon):用来显示一个小图标,常用......
  • C++ OPENGL 贝塞尔曲线绘制
    代码 #include<glad/glad.h>#include<GLFW/glfw3.h>#include<iostream>#include<vector>structPoint{floatx;floaty;};//二次贝塞尔Pointbezier(floatt,Pointa,Pointc,Pointz){return{(1-t)*(1......
  • C++自定义双向迭代器
    #include<cassert>#include<memory>#include<vector>#include<iostream>classRange{public:usingIndex=uint64_t;usingSignedIndex=int64_t;usingOffset=int64_t;usingSize=uint64_t;Range()=d......
  • CS50P: 5. Unit Tests
    assertPython:assert.calculator.py:defmain():x=int(input("What'sx?"))print("xsquaredis",square(x))defsquare(n):returnn+n #刻意为之if__name__=="__main__":main()test_calculator.py:f......
  • consul本地kv数据持久化
    在macOS上(linux同理),如果你希望Consul在重启后能够保留KV数据,可以通过以下步骤配置Consul使用持久化存储。使用文件系统作为后端存储你可以将Consul配置为使用本地文件系统来持久化KV数据。下面是具体的步骤:1.创建数据存储目录首先,创建一个目录来存储Consul的数据。这个目录......
  • python 基础之 scrapy 当当数据一演示
    Items程序importscrapyclassDangdangItem(scrapy.Item):#definethefieldsforyouritemherelike:#name=scrapy.Field()src=scrapy.Field()name=scrapy.Field()price=scrapy.Field()spider程序importscrapyclassDangSpider(......
  • 【THM】Pickle Rick练习
    【THM】PickleRick练习与本文相关的TryHackMe实验房间链接:TryHackMe|Roomdetails简介:瑞克和莫蒂CTF。帮助瑞克变回人类!这个以瑞克和莫蒂为主题的挑战要求你利用网络服务器,找到三种材料来帮助瑞克制作他的药水,并把自己从腌黄瓜变回人类。在此任务上部署虚拟机并浏览We......
  • LCOS技术
    LCOS显示原理1Lcos产生及原理单晶硅有很高的载流子迁移率,但其制程温度与玻璃基板的温度差距太大,无法用于玻璃基板上,因此,有必要使用寻找到新的制程材料以替代。Lcos:LiquidCrystalonSiliconLocs包含CMOS驱动背板、液晶层、配向板、透明电极、偏振片。原理同普通液晶显示,在......
  • 量化交易:如何在QMT中运行Python策略并在VSCode中高效调试?
    哈喽,大家好,我是木头左!为何选择QMT和VSCode进行量化策略开发?在量化交易的世界里,选择正确的工具与拥有优秀的策略同等重要。调用用VisualStudioCode(简称VSCode)或pycharm,方式都差不多。结合QMT的数据处理能力和VSCode的便捷调试功能,可以极大地提高量化策略的开发效率和质量。......