C#之配置文件Ini

简介: C#之配置文件Ini

1、IniConfig类


class IniConfig
    {
        public string path="./log.ini";             //INI文件名 
        //声明写INI文件的API函数
        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        //声明读INI文件的API函数
        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        //类的构造函数,传递INI文件的路径和文件名
        //public IniConfig(string INIPath)
        //{
        //    path = INIPath;
        //}
        //写INI文件
        public void IniWriteValue(string Section, string Key, string Value)
        {
            if (!File.Exists(path))
            {
                //File.Create(path);
                FileStream fs = new FileStream(path, FileMode.CreateNew);
                fs.Close();
            }
            WritePrivateProfileString(Section, Key, Value, path);
        }
        //读取INI文件
        public string IniReadValue(string Section, string Key)
        {
            if (!File.Exists(path))
            {
                return "";
            }
            StringBuilder temp = new StringBuilder(4096);
            int i = GetPrivateProfileString(Section, Key, "", temp, 4096, path);
            return temp.ToString();
        }
    }

其中注意两点:


如果软件只有一个ini文件,那么IniConfig里面的path可以设置为固定的路径,如果炫耀读取不同的ini文件,则使用注释掉的public IniConfig(string INIPath)的。实例化的时候加上路径即可IniConfig logini = new IniConfig("logs.ini");

在写操作时,如果没有该ini配置文件应该创建一个再写,若使用File.Create(path);创建之后不能立马写入,所以使用FileStream fs = new FileStream(path, FileMode.CreateNew);fs.Close();创建ini文件。

2、读取ini


IniConfig iniConfig_log = new IniConfig();
string imgs_dir = iniConfig_log.IniReadValue("objectTest", "imgDir");
if (!String.IsNullOrEmpty(imgs_dir) & Directory.Exists(imgs_dir))
{
    Console.WriteLine(imgs_dir);
}

3、写入ini


string onnx_path = "C:/Users/deploy.onnx";
iniConfig_log.IniWriteValue("objectTest", "onnxPath", onnx_path);

示例的ini文件内容如下:


去.png



 


相关文章
|
5月前
|
数据库连接 C# 数据库
C#数据库连接配置文件存放至App.Config
将C#数据库连接配置文件存放到外置的App.config文件中,并且演示vs和Rider如何读取配置文件连接数据库
105 0
|
XML C# 数据格式
C#应用程序配置文件
C#应用程序配置文件
95 0
C#应用程序配置文件
|
XML C# 数据格式
c#读取配置文件,C#读xml配置文件,c# 配置文件,C# 读取xml
c#读取配置文件,C#读xml配置文件,c# 配置文件,C# 读取xml
618 0
|
C#
C# 不重启程序修改并保存配置文件(appSettings节点)
原文:C# 不重启程序修改并保存配置文件(appSettings节点) private static void UpdateAppConfig(string newKey, string newValue) { bool isModified = false; foreach (string key in ConfigurationManager.
1592 0
|
.NET C# 开发框架
C# 应用程序配置文件操作
应用程序配置文件,对于asp.net是 web.config对于WINFORM程序是 App.Config(ExeName.exe.config)。   配置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.NET 2.0 开始,就非常方便了,提供了 System [.Web] .Configuration 这个管理功能的NameSpace,要使用它,需要添加对 System.configuration.dll的引用。
1071 0
|
XML 数据库连接 C#
C# WinForm程序的App.Config数据库连接配置文件
App.Config【应用程序配置文件】,它其实就是一个标准的XML文件,不过.Net类库已经封装了读取这个文件的方法。可以很方便的使用。看下使用过程。 1.右键解决方案资源管理器中你的项目名,【添加】>【新建项】,选择列表中的【应用程序配置文件】,默认文件即是App.Config。确定。 2.打开App.Config,初始的XML代码为: <xml version=
3400 0
|
.NET C# 开发框架
C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config)。
870 0