雖然微軟早已經建議在WINDOWS中用註冊表代替INI文件,但是在實際應用中,INI文件仍然有用武之地,尤其現在綠色軟件的流行,越來越多的程序將自己的一些配置信息保存到了INI文件中。
INI文件是文本文件,由若干節(section)組成,在每個帶括號的標題下面,是若干個關鍵詞(key)及其對應的值(Value):
[Section] Key=Value
VC中提供了API函數進行INI文件的讀寫操作,但是微軟推出的C#編程語言中卻沒有相應的方法,下面我介紹一個讀寫INI文件的C#類並利用該類保存窗體的座標,當程序再次運行的時候,窗體將顯示在上次退出時的位置。
INIFILE類:
using System; using System.IO; using System.Runtime.InteropServices;
因爲我們需要調用API函數,所以必須創建System.Runtime.InteropServices命名空間以提供可用於訪問 .NET 中的 COM 對象和本機 API 的類的集合。
- using System.Text;
- namespace Ini
- {
- public class IniFile
- {
- public string path; //INI文件名
- [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);
- //聲明讀寫INI文件的API函數
- public IniFile(string INIPath)
- {
- path = INIPath;
- }
- //類的構造函數,傳遞INI文件名
- publicvoid IniWriteValue(string Section,string Key,string Value)
- {
- WritePrivateProfileString(Section,Key,Value,this.path);
- }
- //寫INI文件
- publicstring IniReadValue(string Section,string Key)
- {
- StringBuilder temp = new StringBuilder(255);
- int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);
- return temp.ToString();
- }
- //讀取INI文件指定
- }
- }
調用INIFILE類:
新建一個標準的C# WINDOWS應用程序項目,在窗體中分別增加命名爲sect、key、val的三個文本框。
增加如下代碼:
using Ini; //創建命名空間 //當窗體關閉時保存窗體座標
- privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- IniFile ini = new IniFile("C://test.ini");
- ini.IniWriteValue("LOC" ,"x" ,this.Location.X.ToString());
- ini.IniWriteValue("LOC " ,"y" ,this.Location.Y.ToString());
- //ToString方法將數字轉換爲字符串
- }
//當窗體啓動時,讀取INI文件的值並賦值給窗體
- privatevoid Form1_Load(object sender, System.EventArgs e)
- {
- IniFile ini = new IniFile("C://test.ini");
- Point p=new Point();
- //判斷返回值,避免第一次運行時爲空出錯
- if ((ini.IniReadValue ("LOC" ,"x" )!="" ) && (ini.IniReadValue ("LOC" ,"y" )!=""))
- {
- p.X=int.Parse (ini.IniReadValue ("LOC" ,"x" ));
- p.Y =int.Parse (ini.IniReadValue ("LOC" ,"y" ));
- // int.Parse將字符串轉換爲int
- this.Location =p;
- }
- }
==============================================
其他方法:
DllImport("kernel32.dll")] public extern static int GetPrivateProfileString(string segName, string keyName, string sDefault, StringBuilder buffer, int nSize, string fileName);
public extern static int GetPrivateProfileStringA(string segName, string keyName, string sDefault, byte[] buffer, int iLen, string fileName); // ANSI版本
[DllImport("kernel32.dll")] public extern static int GetPrivateProfileSection(string segName, StringBuilder buffer, int nSize, string fileName);
[DllImport("kernel32.dll")] public extern static int WritePrivateProfileSection(string segName, string sValue, string fileName);
[DllImport("kernel32.dll")] public extern static int WritePrivateProfileString(string segName, string keyName, string sValue, string fileName);
[DllImport("kernel32.dll")] public extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);
- // 封裝的方法中,最有價值的是獲取所有Sections和所有的Keys,網上關於這個的代碼大部分是錯誤的,這裏給出一個正確的方法:
- /// 返回該配置文件中所有Section名稱的集合
- public ArrayList ReadSections()
- {
- byte[] buffer = new byte[65535];
- int rel = 0;// GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), _FileName);
- 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;
- }
- // 獲取節點的所有KEY值
- public ArrayList ReadKeys(string sectionName)
- {
- byte[] buffer = new byte[5120];
- int rel = 0;// GetPrivateProfileStringA(sectionName, null, "", buffer, buffer.GetUpperBound(0), _FileName);
- 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;
- }