ini配置文件的读取

时间:2022-11-05 04:29:58
.ini 文件是Initialization File的缩写,即初始化文件。是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理界面就可实现相同的配置了,但在某些情况,还是要直接编辑.ini才方便,一般只有很熟悉windows才能去直接编辑。开始时用于WIN3X下面,WIN95用注册表代替,以及后面的内容表示一个节,相当于注册表中的键。
现在我们的电脑里面已经用注册表来代替了ini文件的配置,但是我们在一些程序里面ini文件还是无法代替的
有一个setting.ini文件在运用程序.exe同目录下
LANG]
language=zh-Hans
[LOGIN]
serverIP=你需要的服务器IP地址
userName=chaoyue
userPass=000000
[MAPINFO]
web1lat=34.2638
web1lng=108.9469
web2lat=34.2638
web2lng=108.9469
web1zoom=10
web2zoom=10
web1southWestlat=10.055402736564236
web1northEastlat=53.067626642387374
web1southWestlng=58.798828125
web1northEastlng=159.08203125
web2southWestlat=23.32208001137843
web2northEastlat=43.96119063892024
web2southWestlng=81.298828125
web2northEastlng=136.669921875
ini文件的格式
 
格式  INI文件由节、键、值组成。

  [section]

参数(键=值)

  name=value

注解

  注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。
 
  ; comment textINI文件的数据格式的例子(配置文件的内容) [Section1 Name]
 
  KeyName1=value1
 
  KeyName2=value2
 
  ...
 
  [Section2 Name]
 
  KeyName21=value21
 
  KeyName22=value22
 
  其中:
 
  [Section1 Name]用来表示一个段落。
 
  因为INI文件可能是项目*用的,所以使用[Section Name]段名来区分不同用途的参数区。例如:[Section1 Name]表示传感器灵敏度参数区;[Section2 Name]表示测量通道参数区等等。
 
  KeyName1=value1 用来表示一个参数名和值。
 
  比如:
 
  7033=50
 
  7034=51
 
  其中:
 
  7033表示某传感器名,50表示它的灵敏度值。
 
  7034表示另一只传感器名,51表示它的灵敏度值。
 
了解了ini的文件格式了,那么现在就用一个类来操作它
cIni:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace 你的命名空间
{
    class CIni
    {
        //声明读写INI文件的API函数    
        public string path;
        public CIni()
        {
        }
        [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);
        /// <summary>
        /// ini文件编写器
        /// </summary>
        /// <param name="section">查找ini文件的节点[]名</param>
        /// <param name="key">节点下边的键</param>
        /// <param name="val">节点的值</param>
        /// <param name="filePath">来自的文件</param>
        public void WriterINI(string section, string key, string val,string filePath)
        {
            path = System.Environment.CurrentDirectory + "\\" + filePath;
            WritePrivateProfileString(section, key, val, path);
        }
        /// <summary>
        /// 读取Ini文件
        /// </summary>
        /// <param name="section">获得节点</param>
        /// <param name="key">节点下边的键</param>
        /// <param name="filePath">文件路径</param>
        /// <returns>返回的值</returns>
        public string ReadINI(string section, string key, string filePath)
        {
            path = System.Environment.CurrentDirectory + "\\" + filePath;
            StringBuilder temp = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", temp, 255, path);
            return temp.ToString().Trim();
        }
    }
}
用法:
    /// <summary>
        /// 读取Ini文件
        /// </summary>
        /// <param name="strConet">字段</param>
        /// <param name="order"></param>
        private void WriterINI(string strConet, int order)
        {
            GetNextToken(ref strConet, ",");
            CIni ini = new CIni();
            if (order == 1)
            {
                ini.WriterINI("MAPINFO", "web1lat", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web1lng", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web1zoom", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web1southWestlat", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web1northEastlat", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web1southWestlng", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web1northEastlng", GetNextToken(ref strConet, ","), "Setting.ini");
            }
            else
            {
                ini.WriterINI("MAPINFO", "web2lat", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web2lng", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web2zoom", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web2southWestlat", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web2northEastlat", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web2southWestlng", GetNextToken(ref strConet, ","), "Setting.ini");
                ini.WriterINI("MAPINFO", "web2northEastlng", GetNextToken(ref strConet, ","), "Setting.ini");
            }
        }
////GetNextToken方法
   private string GetNextToken(ref string strContent, string strDelim)
        {
            string str = "";
            int idx = strContent.IndexOf(strDelim, 0, strContent.Length);
            if (idx > 0)
            {
                str = strContent.Substring(0, idx);
                strContent = strContent.Remove(0, idx + 1);
            }
            else
            {
                str = strContent;
            }
            return str;
        }
        /// <summary>
        /// 设置ini文件
        /// </summary>
        private void SetCenter()
        {
            CIni ini = new CIni();
            string strweb1lat = ini.ReadINI("MAPINFO", "web1lat", "Setting.ini");
            string strweb1lng = ini.ReadINI("MAPINFO", "web1lng", "Setting.ini");
            string strweb1zoom = ini.ReadINI("MAPINFO", "web1zoom", "Setting.ini");
            string strweb2lat = ini.ReadINI("MAPINFO", "web2lat", "Setting.ini");
            string strweb2lng = ini.ReadINI("MAPINFO", "web2lng", "Setting.ini");
            string strweb2zoom = ini.ReadINI("MAPINFO", "web2zoom", "Setting.ini");
            //你的方法写在这里.....          
        }
 以前常用ini来做配置文件,现在打都是在用config 来作为配置文件