INI配置文件的读写

INI文件是以键值对的形式存储的。由节、键、值组成。

[section]
参数1(键=值)
name1=value1
参数2(键=值)
name2=value2
······

public class INIHelper
    {
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string appName,string keyName,string defaultValue,StringBuilder result,int nSize,string fileName);

        [DllImport("kernel32")]
        private static extern int WritePrivateProfileString(string appName, string keyName, string strWrite, string fileName);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileStringA(string appName, string keyName, string defaultValue, Byte[] result, int nSize, string fileName);

		//读取值
        public static string Read(string section,string key,string def,string filePath)
        {
            StringBuilder strBuil = new StringBuilder(200);
            GetPrivateProfileString(section, key, def, strBuil, 200, filePath);
            return strBuil.ToString();
        }
        
		//写入值
        public static int Write(string section, string key, string value, string filePath)
        {
            return WritePrivateProfileString(section, key, value, filePath);
        }

		//获取所有的键
        public static List<string> ReadAllKeys(string section,string filePath)
        {
            List<string> listResult = new List<string>();
            Byte[] buf = new Byte[65536];

            int len=GetPrivateProfileStringA(section, null, null, buf, buf.Length, filePath);
            int j = 0;
            for (int i = 0; i < len; i++)
            {
                if (buf[i] == 0)
                {
                    listResult.Add(Encoding.Default.GetString(buf, j, i - j));
                    j = i + 1;
                }
            }

            return listResult;
        }
        
		//获取所有的节点
        public static List<string> ReadAllSections(string filePath)
        {
            List<string> listResult = new List<string>();
            Byte[] buf = new Byte[65536];

            int len = GetPrivateProfileStringA(null, null, null, buf, buf.Length, filePath);
            int j = 0;
            for (int i = 0; i < len; i++)
            {
                if (buf[i] == 0)
                {
                    listResult.Add(Encoding.Default.GetString(buf, j, i - j));
                    j = i + 1;
                }
            }

            return listResult;
        }

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c ini配置文件是一种常见的配置文件方式。在C语言中,通常使用文件操作函数来ini配置文件ini文件首先需要打开文件,可以使用fopen函数打开文件,并指定打开方式为"r"(只方式)。然后逐行文件内容,可以使用fgets函数逐行取。取到的每一行字符串都可以通过字符串处理函数进行进一步操作,例如使用strtok函数将行字符串分割成键值对。 对于每一行的键值对,可以进一步使用字符串处理函数进行解析。可以使用strchr函数找到等号(=)的位置,将键和值分隔开。然后可以使用strcpy或strncpy函数将键和值分别复制到变量中,并进行相应的后续处理。 ini文件也需要打开文件,可以使用fopen函数打开文件,并指定打开方式为"w"(入方式)。然后可以使用fprintf函数将配置文件。具体的操作是先入键的字符串,然后入等号(=),最后入值的字符串。入完毕后,可以使用fclose函数关闭文件。 需要注意的是,在取和ini文件时,需要进行错误处理,例如检查文件是否打开成功、是否成功文件关闭时是否出错等。这样可以保证程序的健壮性。 总之,对于C语言来说,ini配置文件是一种比较简单和常见的操作,通过使用文件操作函数和字符串处理函数,可以方便地取和ini文件中的配置项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值