首页 > 代码库 > C# 读取和配置IniFile

C# 读取和配置IniFile

自定义IniFile操作类

技术分享
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.IO;namespace Sisley{    public  class INIFileHepler    {               private string  Default_IniFileName = "Config.ini"; //INI文件名        private string strIniFilePath;        // 返回0表示失败,非0为成功          [DllImport("kernel32", CharSet = CharSet.Auto)]        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);        // 返回取得字符串缓冲区的长度          [DllImport("kernel32", CharSet = CharSet.Auto)]        private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath);        /*         [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]         public static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath);          */        public INIFileHepler()          {            this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" +Default_IniFileName;         }   public INIFileHepler(string strIniFilePath)        {            if  ( Directory.Exists(strIniFilePath))            {                this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" + Default_IniFileName;            }  this.strIniFilePath = strIniFilePath;        }               /// <summary>        /// 写入        /// </summary>        /// <param name="section">写入section</param>        /// <param name="key">写入key</param>        /// <param name="value">写入value</param>        public void  WriteIniString(string section, string key, string value)          {            try            {                WritePrivateProfileString(section, key, value, strIniFilePath);            }            catch            {                throw new Exception("配置文件不存在或权限不足导致无法写入");            }                  }        /// <summary>          /// 获取ini配置文件中的字符串          /// </summary>          /// <param name="section">节名</param>          /// <param name="key">键名</param>          /// <param name="strDefault">默认值</param>          /// <param name="retVal">结果缓冲区</param>          /// <param name="size">结果缓冲区大小</param>          /// <returns>返回值</returns>          public string  GetIniString(string section, string key, string strDefault, StringBuilder retVal, int size)        {            long liRet = GetPrivateProfileString(section, key, strDefault, retVal, size, strIniFilePath);            //return (liRet >= 1);            StringBuilder temp = new StringBuilder(1024);            INIFileHepler.GetPrivateProfileString(section, key, strDefault, temp, 1024, strIniFilePath);            return temp.ToString();        }      }}
View Code

使用示例:

设置和设置

技术分享
  INIFileHepler inifile = new INIFileHepler();        //INIFileHepler inifile = new INIFileHepler(@"D:\MyDocs\CSharp\Program\bin\Debug\TestConfig.ini");        private void button1_Click(object sender, EventArgs e)        {                                inifile.WriteIniString("DatabaseConfig", "Server", "192.168.10.3");            inifile.WriteIniString("DatabaseConfig", "DBName", "DB_Live");            inifile.WriteIniString("DatabaseConfig", "UserName", "admin");            inifile.WriteIniString("DatabaseConfig", "UserPassword", "123456");        }        private void button2_Click(object sender, EventArgs e)        {            richTextBox1.AppendText("DatabaseConfig"+"\n");            StringBuilder Server = new StringBuilder(32);            StringBuilder DBName = new StringBuilder(32);            StringBuilder UserName = new StringBuilder(32);            StringBuilder UserPassword = new StringBuilder(32);            inifile.GetIniString("DatabaseConfig", "Server", "", Server, Server.Capacity);            inifile.GetIniString("DatabaseConfig", "DBName", "", DBName, DBName.Capacity);            inifile.GetIniString("DatabaseConfig", "UserName", "", UserName, UserName.Capacity);            inifile.GetIniString("DatabaseConfig", "UserPassword", "", UserPassword, UserPassword.Capacity);                        richTextBox1.AppendText("Server:   " +Server+  "\n");            richTextBox1.AppendText("DBName:   " + DBName + "\n");            richTextBox1.AppendText("UserName:   " + UserName + "\n");            richTextBox1.AppendText("UserPassword:   " + UserPassword + "\n");   }
View Code

 

C# 读取和配置IniFile