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
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"); //你的方法写在这里..... }
原文:http://www.cnblogs.com/imeiba/p/5698019.html