重构YH是遇到这个一样问题,就是YH前台页面的按钮要求实现客户自定义,客户可以跟随自己的喜好安排每个按钮的功能,所以,在每一次客户登陆是就需要初始化客户自定义排版,这里就需要用到初始化文件——INI。
.ini 文件是Initialization File的缩写,即初始化文件。是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理界面就可实现相同的配置了,但在某些情况,还是要直接编辑.ini才方便,一般只有很熟悉windows才能去直接编辑。开始时用于WIN3X下面,WIN95用注册表代替,以及后面的内容表示一个节,相当于注册表中的键。
除了windows现在很多其他操作系统下面的应用软件也有.ini文件,用来配置应用软件以实现不同用户的要求。一般不用直接编辑这些.ini文件,应用程序的图形界面即可操作以实现相同的功能。它可以用来存放软件信息,注册表信息等。
INI文件由节、键、值组成。
节
[section]
参数(键=值)
name=value
既然ini文件在初始化中使用,那么免不了的需要对它进行一些写入读取修改的操作,当然,这些东西window已经帮我们封装好了,我们只需要调用就可以了。很easy。
Ini使用到的一些API函数:
- #region 调用windowsAPI
- //用来读取INI 文件的内容
- [DllImport("Kernel32.dll")]
- private static extern int GetPrivateProfileString(string strAppName,string strKeyName,string strDefault,StringBuilder sbReturnString,int nSize,string strFileName);
- //返回所读取的字符串值的真实长度
- [DllImport("Kernel32.dll")]
- private extern static int GetPrivateProfileStringA(string strAppName, string strKeyName, string sDefault, byte[] buffer, int nSize, string strFileName);
- [DllImport("Kernel32.dll")]
- private static extern int GetPrivateProfileInt(string strAppName, string strKeyName, int nDefault, string strFileName);
- //获取ini文件所有的section
- [DllImport("Kernel32.dll")]
- private extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);
- //获取指定Section的key和value
- [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
- private static extern int GetPrivateProfileSection(string lpAppName,byte[] lpReturnedString, int nSize,string lpFileName);
- //根据传入参数的不同进行写入或修改或删除操作(返回值 Long,非零表示成功,零表示失败)
- [DllImport("Kernel32.dll")]
- public static extern long WritePrivateProfileString(string strAppName, string strKeyName, string strKeyValue, string strFileName);
- //添加一个section内容列表
- [DllImport("Kernel32.dll")]
- public static extern long WritePrivateProfileSection(string strAppName, string strkeyandvalue, string strFileName);
- #endregion
调用这些函数的所用到的方法:
- #region 供UI调用的方法
- /// <summary>
- /// 判断该ini文件是否存在如果不存在新建一个该文件
- /// </summary>
- public void FileExists()
- {
- try
- {
- if (!File.Exists(this.filePath))
- {
- using (FileStream fs = File.Create(this.filePath))
- {
- fs.Close();
- }
- }
- }
- catch(Exception e)
- {
- }
- }
- /// <summary>
- /// 返回该配置文件中所有Section名称的集合
- /// </summary>
- /// <returns></returns>
- public ArrayList ReadSections()
- {
- byte[] buffer = new byte[65535];
- int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), this.filePath);
- int iCnt, iPos;
- ArrayList arrayList = new ArrayList();
- string tmp;
- if (rel > 0)
- {
- iCnt = 0; iPos = 0;
- for (iCnt = 0; iCnt < rel; iCnt++)
- {
- if (buffer[iCnt] == 0x00)
- {
- tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();
- iPos = iCnt + 1;
- if (tmp != "")
- arrayList.Add(tmp);
- }
- }
- }
- return arrayList;
- }
- /// <summary>
- /// 获取指定节点的所有KEY的名称
- /// </summary>
- /// <param name="sectionName"></param>
- /// <returns></returns>
- public ArrayList ReadKeys(string sectionName)
- {
- byte[] buffer = new byte[5120];
- int rel = GetPrivateProfileStringA(sectionName, null, "", buffer, buffer.GetUpperBound(0), this.filePath);
- int iCnt, iPos;
- ArrayList arrayList = new ArrayList();
- string tmp;
- if (rel > 0)
- {
- iCnt = 0; iPos = 0;
- for (iCnt = 0; iCnt < rel; iCnt++)
- {
- if (buffer[iCnt] == 0x00)
- {
- tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();
- iPos = iCnt + 1;
- if (tmp != "")
- arrayList.Add(tmp);
- }
- }
- }
- return arrayList;
- }
- /// <summary>
- /// 读取指定节点下的指定key的value返回string
- /// </summary>
- /// <param name="section"></param>
- /// <param name="key">param>
- /// <returns></returns>
- public string GetIniKeyValueForStr(string section, string key)
- {
- if (section.Trim().Length <= 0 || key.Trim().Length <= 0) return string.Empty;
- StringBuilder strTemp = new StringBuilder(256);
- GetPrivateProfileString(section, key, string.Empty, strTemp, 256, this.filePath);
- return strTemp.ToString().Trim();
- }
- /// <summary>
- /// 从指定的节点中获取一个整数值( Long,找到的key的值;如指定的key未找到,就返回默认值。如找到的数字不是一个合法的整数,函数会返回其中合法的一部分。如,对于“xyz=55zz”这个条目,函数返回55。)
- /// </summary>
- /// <param name="section"></param>
- /// <param name="key"></param>
- /// <returns></returns>
- public int GetIniKeyValueForInt(string section, string key)
- {
- if (section.Trim().Length <= 0 || key.Trim().Length <= 0) return 0;
- return GetPrivateProfileInt(section, key, 0, this.filePath);
- }
- /// <summary>
- /// 读取指定节点下的所有key 和value
- /// </summary>
- /// <param name="section"></param>
- /// <returns></returns>
- public ArrayList GetIniSectionValue(string section)
- {
- byte[] buffer = new byte[5120];
- int rel = GetPrivateProfileSection(section, buffer, buffer.GetUpperBound(0), this.filePath);
- int iCnt, iPos;
- ArrayList arrayList = new ArrayList();
- string tmp;
- if (rel > 0)
- {
- iCnt = 0; iPos = 0;
- for (iCnt = 0; iCnt < rel; iCnt++)
- {
- if (buffer[iCnt] == 0x00)
- {
- tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();
- iPos = iCnt + 1;
- if (tmp != "")
- arrayList.Add(tmp);
- }
- }
- }
- return arrayList;
- }
- /// <summary>
- /// 往指定section的key中写入value
- /// </summary>
- /// <param name="section"></param>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public bool WriteIniKey(string section, string key, string value)
- {
- try
- {
- if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)
- {
- flag = false;
- }
- else
- {
- if (WritePrivateProfileString(section, key, value, this.filePath) == 0)
- {
- flag = false;
- }
- else
- {
- flag = true;
- }
- }
- }
- catch
- {
- flag = false;
- }
- return flag;
- }
- /// <summary>
- /// 修改指定section的key的值
- /// </summary>
- /// <param name="section"></param>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public bool EditIniKey(string section, string key, string value)
- {
- try
- {
- if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)
- {
- flag = false;
- }
- else
- {
- if (WritePrivateProfileString(section, key, value, this.filePath) == 0)
- {
- flag = false;
- }
- else
- {
- flag = true;
- }
- }
- }
- catch
- {
- flag = false;
- }
- return flag;
- }
- /// <summary>
- /// 删除指定section的指定key
- /// </summary>
- /// <param name="section"></param>
- /// <param name="key"></param>
- /// <returns></returns>
- public bool DeleteIniKey(string section, string key)
- {
- try
- {
- if (section.Trim().Length <= 0 || key.Trim().Length <= 0)
- {
- flag = false;
- }
- else
- {
- if (WritePrivateProfileString(section, key, null, this.filePath) == 0)
- {
- flag = false;
- }
- else
- {
- flag = true;
- }
- }
- }
- catch
- {
- flag = false;
- }
- return flag;
- }
- /// <summary>
- /// 删除指定section
- /// </summary>
- /// <param name="section"></param>
- /// <returns></returns>
- public bool DeleteIniSection(string section)
- {
- try
- {
- if (section.Trim().Length <= 0)
- {
- flag = false;
- }
- else
- {
- if (WritePrivateProfileString(section, null, null, this.filePath) == 0)
- {
- flag = false;
- }
- else
- {
- flag = true;
- }
- }
- }
- catch
- {
- flag = false;
- }
- return flag;
- }
- /// <summary>
- /// 给一个节点写入key和value列表
- /// </summary>
- /// <param name="section"></param>
- /// <param name="ht"></param>
- /// <returns></returns>
- public bool WriteIniSectionAndValue(string section, Hashtable ht)
- {
- string lpString = "";
- try
- {
- if (section.Trim().Length <= 0 || ht.Count == 0)
- {
- flag = false;
- }
- else
- {
- foreach (DictionaryEntry de in ht)
- {
- lpString += de.Key+"="+de.Value;
- lpString += "\r\n";
- }
- if (WritePrivateProfileSection(section, lpString, this.filePath) == 0)
- {
- flag = false;
- }
- else
- {
- flag = true;
- }
- }
- }
- catch
- {
- flag = false;
- }
- return flag;
- }
- /// <summary>
- /// 给一个节点写入key 列表
- /// </summary>
- /// <param name="section"></param>
- /// <param name="lstKeyValue"></param>
- /// <returns></returns>
- public bool WriteIniSectionName(string section, List<string> lstKeyValue)
- {
- string lpString = "";
- try
- {
- if (section.Trim().Length <= 0 || lstKeyValue.Count == 0)
- {
- flag = false;
- }
- else
- {
- for (int i = 0; i < lstKeyValue.Count; ++i)
- {
- lpString += lstKeyValue[i];
- lpString += "\r\n";
- }
- if (WritePrivateProfileSection(section, lpString, this.filePath) == 0)
- {
- flag = false;
- }
- else
- {
- flag = true;
- }
- }
- }
- catch
- {
- flag = false;
- }
- return flag;
- }
- #endregion
在U层,我们需要
- //创建一个INIFile对象,参数为文件路径
- ManagerConfigIni iniConfig = new ManagerConfigIni(AppDomain.CurrentDomain.BaseDirectory + @"\配置.ini");
然后通过相应的事件,来调用对应的方法即可。
如果 ini 中没有指定的 Section,API会新建 Section,如果没有指定的 Key则新建一个 Key 并写入数据,如果已经存在,则用字符串代替原来的值。当指定的 ini也不存在的时候,API 会自动建立一个新的文件,所以使用 ini的好处是我们不必为了保存少量的数据涉及到文件操作,就连查找文件是否存在的操作都不必要。
使用要点:在我们实际使用的时候,用的最多的是 GetPrivateProfileString和 WritePrivateProfileString,但在对自定义 ini文件操作的时候要注意的是,如果 lpFileName 指定的文件没有路径的话,Api会去 Windows 的安装目录去找而不会在当前目录找,但是每次用到 ini函数要获取当前路径显然太麻烦了,这里有一个变通的办法,你只要在 ini 文件名前面加上 .\ 就可以了,比如说要对本目录下的 user.ini操作,那么文件名就是 '.\user.ini' 这样显然比较方便。另外,当你要把一个 Key清除的时候,可以使用把 lpString 指向一个空的字符串然后使用 WritePrivateProfileString。当你要把一个 section的全部内容清空的时候,也不必把 key 一个个的清除,可以使用把 lpString指向一个空的字符串然后使用 WritePrivateProfileSection。 INI文件就是扩展名为“ini”的文件。在Windows系统中,INI文件是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。但自从Windows 95的退出,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点,使应用程序和系统都把许多参数和初始化信息放进了注册表中。
http://blog.csdn.net/laner0515/article/details/8439933