在做项目过程中有时需要保存一些简单的配置信息可以使用xml也可以使用INI文件下面是C#中读取INI的方法相信大部分朋友都使用过这种方式
INI文件的存储方式如下
view plainprint?
[section] key=value读取方法
view plainprint?
public static class ReadnIni { private static iniPath=ini的路径public static string IniPath { get {return iniPath} set{ iniPath=value} }
[DllImport(kernel)]
private static extern int GetPrivateProfileString(string iniAppNamestring iniKeyNamestring iniDefaultStringBuilder iniReturnedStringint iniSizestring iniFileName)
private static string ReadString(string section string key string def)
{ StringBuilder temp = new StringBuilder(要读出的内容的长度)try { GetPrivateProfileString(section key def temp 长度 IniPath )} catch {} return tempToString()}
public static Hashtable ReadIniListKeys(string section List<string> keys)
{ Hashtable keyValueHash = new Hashtable()for (int i = i < keysCount i++)
{ keyValueHashAdd(keys[i] ReadString(section keys[i] ))} return keyValueHash}
public static string ReadIniKeys(string section string keys)
{ return ReadString(section keys )}如上就可以读取了那么INI文件如何创建呢?
view plainprint?
[DllImport(kernel)] public static extern long WritePrivateProfileString(string section string key string value string iniPath)