C#读写INI文件

0
C# .NET C/C++ c# 7911 次浏览

C#读写INI文件,需要用到两个API函数

WritePrivateProfileString

GetPrivateProfileString

需要特别注意的是:这两个函数中的*.ini文件地址要使用绝对地址

下面程序的功能,是在一个空文件test.ini中,写入一些属性,并读取
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IniTest
{
    /// <summary>
    /// INI文件工具类
    /// </summary>
    class IniHelper
    {
        /// <summary>
        /// 设定INI文件中的属性
        /// </summary>
        /// <param name="section">节</param>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        /// <param name="filePath">INI文件的绝对地址</param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern long WritePrivateProfileString(
            string section, string key, string val, string filePath);
 
        /// <summary>
        /// 读取INI文件中的属性
        /// </summary>
        /// <param name="section">节</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <param name="retVal">被存储到的StringBuilder</param>
        /// <param name="size">最大字串截取长度</param>
        /// <param name="filePath">INI文件的绝对地址</param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(
            string section, string key, string def, 
            System.Text.StringBuilder retVal, int size, string filePath);
 
        /// <summary>
        /// INI文件的绝对路径
        /// </summary>
        public string Path;
 
        /// <summary>
        /// 读取的默认值
        /// </summary>
        public string DefValue;
 
        /// <summary>
        /// INI读写工具类
        /// </summary>
        /// <param name="path">INI文件绝对地址</param>
        /// <param name="def">读取值的默认值</param>
        public IniHelper(string path, string def = "")
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(path);
            if (!fi.Exists)
            {
                throw new Exception("文件不存在");
            }
            this.Path = fi.FullName;
            this.DefValue = def;
        }
 
        /// <summary>
        /// 设置INI文件的一个值
        /// </summary>
        /// <param name="section">节</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void SetValue(string section, string key, object value)
        {
            WritePrivateProfileString(section, key, value.ToString(), this.Path);
        }
 
        /// <summary>
        /// 获取INI文件的一个值
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValue(string section, string key)
        {
            StringBuilder sb = new StringBuilder(255);
            int i = GetPrivateProfileString(
                section, key, this.DefValue, sb, 255, this.Path);
            return sb.ToString();
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            IniHelper ih = new IniHelper("test.ini", "Default");
 
            //设置INI文件中的值
            ih.SetValue("Set1", "attr1", "1");
            ih.SetValue("Set1", "attr2", 2L);
            ih.SetValue("Set1", "attr3", 3f);
            ih.SetValue("Set2", "attra", new StringBuilder("a"));
            ih.SetValue("Set2", "attrb", 'b');
 
            //读取INI文件中的值
            Console.WriteLine(ih.GetValue("Set1", "attr1"));
            Console.WriteLine(ih.GetValue("Set1", "attr2"));
            Console.WriteLine(ih.GetValue("Set1", "attr3"));
            Console.WriteLine(ih.GetValue("Set2", "attra"));
            Console.WriteLine(ih.GetValue("Set2", "attrb"));
 
            //读取不存在的键值
            Console.WriteLine(ih.GetValue("Setx", "attrx"));
 
            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}
来自:http://my.oschina.net/Tsybius2014/blog/326704
请尽量让自己的答案能够对别人有帮助

0个答案

默认排序 按投票排序