/*---------------------------------------------------------------- // 文 件 名:INIControl.cs // 功能描述:INI文件读写 // // 创建标识:彭玉柱 2010-07-12 //----------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; using System.Collections.Specialized; using System.Globalization; namespace GeneralLib { public class INIControl { private static INIControl iniColtrol; private static string filePath;//INI文件路径 //声明读写INI文件的API函数 [DllImport("kernel32")] private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath); /// /// 构造函数 /// private INIControl() { } /// /// 得到INIControl对象 /// /// INI文件路径,包括文件名 /// INIControl public static INIControl GetInstances(string filePath) { FileInfo fileInfo = new FileInfo(filePath); //判断INI文件是否存在 if (!fileInfo.Exists) { throw (new ApplicationException("Ini文件不存在")); } INIControl.filePath = fileInfo.FullName; return iniColtrol == null ? iniColtrol = new INIControl() : iniColtrol; } /// /// 读取所有的段落(Section)的名称 /// /// StringCollection public void ReadSections(StringCollection SectionList) { //Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section byte[] buffer = new byte[65535]; int bufLen = 0; bufLen = GetPrivateProfileString(null, null, null, buffer, buffer.GetUpperBound(0), filePath); GetStringsFromBuffer(buffer, bufLen, SectionList); } /// /// 字节数组转字符串集合 /// /// /// /// private void GetStringsFromBuffer(Byte[] buffer, int bufLen, StringCollection Strings) { Strings.Clear(); if (bufLen != 0) { int start = 0; for (int i = 0; i < bufLen; i++) { if ((buffer[i] == 0) && ((i - start) > 0)) { String s = Encoding.GetEncoding(0).GetString(buffer, start, i - start); Strings.Add(s); start = i + 1; } } } } /// /// 写INI文件 /// /// 段落名 /// 关键字 /// 值 public void WriteValue(string section, string key, object val) { try { WritePrivateProfileString(section, key, val.ToString(), filePath); } catch (Exception ex) { SystemLog.WriterLog(ex, true); } } /// /// 读字符串 /// /// 段落名 /// 关键字 /// 无法读取时默认的值 /// public string ReadString(string section, string key, string defaultVal) { Byte[] buffer = new Byte[65535]; int bufLen = GetPrivateProfileString(section, key, defaultVal, buffer, buffer.GetUpperBound(0), filePath); //必须设定0(系统默认的代码页)的编码方式,否则无法支持中文 string s = Encoding.GetEncoding(0).GetString(buffer); s = s.Substring(0, bufLen); return s.Trim().TrimEnd('\0');//杨斌 2016-07-06 } /// /// 读整数 /// /// 段落名 /// 关键字 /// 无法读取时默认的值 /// public int ReadInt(string section, string key, int defaultVal) { string val = this.ReadString(section, key, defaultVal.ToString()); try { return Convert.ToInt16(val); } catch (Exception ex) { SystemLog.WriterLog(ex); return defaultVal; } } /// /// 读short值 /// /// 段落名 /// 关键字 /// 无法读取时默认的值 /// public short ReadShort(string section, string key, short defaultVal) { string val = this.ReadString(section, key, defaultVal.ToString()); try { return short.Parse(val); } catch (System.Exception ex) { SystemLog.WriterLog(ex); return defaultVal; } } /// /// 读bool值 /// /// 段落名 /// 关键字 /// 无法读取时默认的值 /// public bool ReadBool(string section, string key, bool defaultVal) { string val = this.ReadString(section, key, defaultVal.ToString()); try { return Convert.ToBoolean(val.ToLower()); } catch (Exception ex) { SystemLog.WriterLog(ex); return defaultVal; } } /// /// 读double值 /// /// 段落名 /// 关键字 /// 无法读取时默认的值 /// public double ReadDouble(string section, string key, double defaultVal) { string val = this.ReadString(section, key, defaultVal.ToString()); val = ConvertOper.DoNumberString(val);//杨斌 2013-08-29 try { return Convert.ToDouble(val); } catch (Exception ex) { SystemLog.WriterLog(ex); return defaultVal; } } } }