日期:2014-06-10  浏览次数:20460 次

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace HelloCSharp
{
    //[section]
    //key=value
    //
    class IniHelper
    {
        /// <summary>
        /// ini文件的路径
        /// </summary>
        private string filePath;


        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public IniHelper(string filePath)
        {
            this.filePath = filePath;
        }

        public void IniWriteValue(string section, string key, string val)
        {
            WritePrivateProfileString(section, key, val, filePath);
        }

        public string IniReadValue(string section, string key)
        {
            StringBuilder temp = new StringBuilder(500);
            GetPrivateProfileString(section, key, "", temp, 500, filePath);
            return temp.ToString();
        }
    }
}