ini配置文件读取类
2010-02-06 14:39
260 查看
整理的 ini 文件读写类
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.IO; namespace DABAOKU { /// <summary> /// 读写ini配置文件类 /// </summary> class IniFile { #region 成员变量 public string filePath = ""; //文件路径 static private uint MaxBufferSize = 32767;//缓存大小 #endregion #region 导入API [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 int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern int GetPrivateProfileString(string section, string key, string def, IntPtr retVal, int size, string filePath); [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath); [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern int GetPrivateProfileSection(string section, IntPtr lpReturnedString, uint nSize, string filePath); [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string filepath); #endregion #region 构造函数 /// <summary> /// 构造函数. /// </summary> /// <PARAM name="INIPath"></PARAM> public IniFile(string filename) { filePath = System.IO.Path.GetFullPath(filename); } #endregion #region 读取数据 public string GetString(string Section, string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.filePath); return temp.ToString(); } public int GetInt(string Section, string Key) { return GetPrivateProfileInt(Section, Key, 0, this.filePath); } public List<KeyValuePair<string, string>> GetValueSetList(string Section) //读取一段内的所有数据 { List<KeyValuePair<string, string>> retval; string[] keyValuePairs; string key, value; int equalSignPos; //申请空间 IntPtr ptr = Marshal.AllocCoTaskMem((int)IniFile.MaxBufferSize); try { int len = GetPrivateProfileSection(Section, ptr, MaxBufferSize, filePath); keyValuePairs = ConvertNullSeperatedStringToStringArray(ptr, len); } finally { Marshal.FreeCoTaskMem(ptr); } //输出结果 retval = new List<KeyValuePair<string, string>>(keyValuePairs.Length); for (int i = 0; i < keyValuePairs.Length; ++i) { equalSignPos = keyValuePairs[i].IndexOf('='); key = keyValuePairs[i].Substring(0, equalSignPos); value = keyValuePairs[i].Substring(equalSignPos + 1, keyValuePairs[i].Length - equalSignPos - 1); retval.Add(new KeyValuePair<string, string>(key, value)); } return retval; } public string[] GetSectionNames() //获得所有段名 { string[] retval; int len; IntPtr ptr = Marshal.AllocCoTaskMem((int)IniFile.MaxBufferSize); try { len = GetPrivateProfileSectionNames(ptr, IniFile.MaxBufferSize, filePath); retval = ConvertNullSeperatedStringToStringArray(ptr, len); } finally { Marshal.FreeCoTaskMem(ptr); } return retval; } public string[] GetKeyNames(string Section) //获得一段内所有关键码 { int len; string[] retval; IntPtr ptr = Marshal.AllocCoTaskMem((int)IniFile.MaxBufferSize); try { len = GetPrivateProfileString(Section, null, null, ptr, (int)IniFile.MaxBufferSize, filePath); retval = ConvertNullSeperatedStringToStringArray(ptr, len); } finally { Marshal.FreeCoTaskMem(ptr); } return retval; } private static string[] ConvertNullSeperatedStringToStringArray(IntPtr ptr, int valLength) //转换字符串指针空间为字符串数组 { string[] retval; if (valLength == 0) { retval = new string[0]; } else { string buff = Marshal.PtrToStringAuto(ptr, valLength - 1); retval = buff.Split('/0'); } return retval; } #endregion #region 写数据 public void SetValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.filePath); } public void SetValue(string Section, string Key, int Value) { WritePrivateProfileString(Section, Key, Value.ToString(), this.filePath); } public void SetValue(string Section, string Key, float Value) { WritePrivateProfileString(Section, Key, Value.ToString(), this.filePath); } public void SetValue(string Section, string Key, double Value) { WritePrivateProfileString(Section, Key, Value.ToString(), this.filePath); } #endregion #region 删除 public void DeleteKey(string Section, string Key) { SetValue(Section, Key, null); } public void DeleteKey(string Section) { SetValue(Section, null, null); } #endregion #region 补充 static public void FormatIni(string filePath) { StreamReader sr = new StreamReader(filePath); StreamWriter sw = new StreamWriter(filePath+".gc1"); string s = ""; while ((s = sr.ReadLine()) != null) { if (s != "" && s.IndexOf("[") == -1 && s.IndexOf("=") == -1) s += "="; sw.WriteLine(s); } sr.Close(); sw.Close(); try { File.Move(filePath, filePath + ".gc"); } catch(System.Exception ex){} try { File.Move(filePath+".gc1", filePath); } catch (System.Exception ex){} try { File.Delete(filePath+".gc1"); } catch(System.Exception ex){} } #endregion } }
相关文章推荐
- python读取/写入配置文件ini方法
- 利用GetPrivateProfileString读取配置文件(.ini)
- C#读取写入配置文件Cinfig.ini方法
- shell脚本读取ini配置文件的值
- 利用GetPrivateProfileString读取配置文件(.ini)
- windows下读取配置文件(.ini)
- php读取ini配置文件属性
- 利用GetPrivateProfileString读取配置文件(.ini)
- PHP读取4种配置文件(php,ini,yaml,xml)
- [导入]ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- QT读取ini配置文件(QSettings)
- 批处理读取ini配置文件
- 读取INI配置文件内容(源文件C)
- shell脚本读取ini配置文件的值
- Linux下用C读取INI配置文件
- Linux下用C读取INI配置文件
- 读取ini配置文件的API
- 利用GetPrivateProfileString读取配置文件(.ini)
- MFC中如何读取.ini配置文件
- C#中读取ini配置文件 [DllImport("kernel32.dll")]