详解c#如何实现读写ini文件-kb88凯时官网登录

来自:网络
时间:2023-01-01
阅读:
免费资源网 - https://freexyz.cn/
目录

介绍

ini文件格式由节、键、值组成。

[section]

参数

(键=值)

name=value

1.创建一个ini文件

在debug目录下创建一个ini文件,写入下面内容,注意编码格式用ansi。

[information]

name=周星星

gender=男

age=55

region=香港

2.创建一个winform界面

如下图

3.添加一个ini管理类

using system;
using system.collections.generic;
using system.linq;
using system.runtime.interopservices;
using system.text;
using system.threading.tasks;
 
namespace test1
{
   public static class inifunc
    {
        /// 
        /// 获取值
        /// 
        /// 段落名
        /// 键名
        /// 读取异常是的缺省值
        /// 键名所对应的的值,没有找到返回空值
        /// 返回值允许的大小
        /// ini文件的完整路径
        /// 
        [dllimport("kernel32.dll")]
        private static extern int getprivateprofilestring(
            string section,
            string key,
            string defval,
            stringbuilder retval,
            int size,
            string filepath);
 
        /// 
        /// 写入
        /// 
        /// 需要写入的段落名
        /// 需要写入的键名
        /// 写入值
        /// ini文件的完整路径
        /// 
        [dllimport("kernel32.dll")]
        private static extern int writeprivateprofilestring(
            string section,
            string key,
            string val,
            string filepath);
 
 
        /// 
        /// 获取数据
        /// 
        /// 段落名
        /// 键名
        /// 没有找到时返回的默认值
        /// ini文件完整路径
        /// 
        public static string getstring(string section, string key, string def, string filename)
        {
            stringbuilder sb = new stringbuilder(1024);
            getprivateprofilestring(section, key, def, sb, 1024, filename);
            return sb.tostring();
        }
 
        /// 
        /// 写入数据
        /// 
        /// 段落名
        /// 键名
        /// 写入值
        /// ini文件完整路径
        public static void writestring(string section, string key, string val, string filename)
        {
            writeprivateprofilestring(section, key, val, filename);
        }
    }
}

4.添加winform代码

双击winform界面,加入下面代码

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
 
namespace test1
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
        }
 
        private string filename = null;
 
        private void form1_load(object sender, eventargs e)
        {
            filename = application.startuppath   "\\config.ini";
        }
 
        /// 
        /// 读取
        /// 
        /// 
        /// 
        private void button_read_click(object sender, eventargs e)
        {
            string names = inifunc.getstring("information", "name", null, filename);
            string gender = inifunc.getstring("information", "gender", null, filename);
            string age = inifunc.getstring("information", "age", null, filename);
            string region = inifunc.getstring("information", "region", null, filename);
 
            textbox_name.text = names;
            textbox_gender.text = gender;
            textbox_age.text = age;
            textbox_region.text = region;
        }
 
        /// 
        /// 写入
        /// 
        /// 
        /// 
        private void button_write_click(object sender, eventargs e)
        {
            string names = textbox_name.text;
            string gender = textbox_gender.text;
            string age = textbox_age.text;
            string region = textbox_region.text;
 
            inifunc.writestring("information", "name", names, filename);
            inifunc.writestring("information", "gender", gender, filename);
            inifunc.writestring("information", "age", age, filename);
            inifunc.writestring("information", "region", region, filename);
        }
 
        /// 
        /// 清空
        /// 
        /// 
        /// 
        private void button_clear_click(object sender, eventargs e)
        {
            textbox_name.text = string.empty;
            textbox_gender.text = string.empty;
            textbox_age.text = string.empty;
            textbox_region.text = string.empty;
        }
    }
}
免费资源网 - https://freexyz.cn/
返回顶部
顶部
网站地图