在c#編程中,某些情況下我們可能還會用到.ini文件。例如為一個輸入界面創(chuàng)建“動態(tài)幫助”:
我們在輸入界面下方設(shè)置一個標(biāo)簽,當(dāng)用戶將光標(biāo)移動到每一個textbox或其他輸入,選擇框時,標(biāo)簽文字自動變換為該輸入項的一些幫助信息。
ini文件是文本文件,由若干節(jié)(section)組成,在每個帶括號的標(biāo)題下面,是若干個關(guān)鍵詞(key)及其對應(yīng)的值(value)
[section]
key=value
我們的ini文件比較簡單,文件名為:helpinfo.ini。
-----------------
[promptinfo]
yourtextbox = 請輸入xxxxx信息。
-----------------
首先我們要用以下語句調(diào)用kernel32.dll
[dllimport ("kernel32")]
private static extern int getprivateprofilestring(string section, string key, string def, stringbuilder retval, int size, string filepath);
然后為輸入項(如一個textbox)的enter事件編寫一個回調(diào)方法
this.yourtextbox.enter += new system.eventhandler(this.conenter);
private void conenter(object sender, system.eventargs e)
{
string strpromptfile = directory.getcurrentdirectory() + "//helpinfo.ini";//獲取ini文件所在的路徑
string strclsname = sender.gettype().tostring().toupper();
if( strclsname.endswith("textbox") )
{
if(sender.equals(yourtextbox))
{
getprivateprofilestring("promptinfo", "yourtextbox" ,"",strpromptcontent,
1024, strpromptfile);
}
txthelpcontent.text = strpromptcontent.tostring();//txthelpcontent就是顯示幫助信息的標(biāo)簽
}
菜鳥學(xué)堂: