內容:
ini檔案在系統配置及應用程式引數儲存與設定方面,具有很重要的作用,所以視覺化的程式設計一族,如vb、vc、vfp、delphi等都提供了讀寫ini檔案的方法,其中delphi中操作ini檔案,最為簡潔,這是因為delphi6.0提供了乙個tinifile類,使我們可以非常靈活的處理ini檔案。
一、有必要了解ini檔案的結構:
;注釋[小節名]
關鍵字=值
...ini檔案允許有多個小節,每個小節又允許有多個關鍵字, 「=」後面是該關鍵字的值。
值的型別有三種:字串、整型數值和布林值。其中字串存貯在ini檔案中時沒有引號,布林真值用1表示,布林假值用0表示。
注釋以分號「;」開頭。
二、定義
1、在inte***ce的uses節增加inifiles;
2、在var變數定義部分增加一行:
myinifile:tinifile;
然後,就可以對變數myinifile進行建立、開啟、讀取、寫入等操作了。
三、開啟ini檔案
myinifile:=tinifile.create(』program.ini』);
上面這一行語句將會為變數myinifile與具體的檔案 program.ini建立聯絡,然後,就可以通過變數myinifile,來讀寫program.ini檔案中的關鍵字的值了。
值得注意的是,如果括號中的檔名沒有指明路徑的話,那麼這個program.ini檔案會儲存在windows目錄中,把program.ini檔案儲存在應用程式當前目錄中的方法是:為其指定完整的路徑及檔名。下面的兩條語句可以完成這個功
能: filename:=extractfilepath(paramstr(0))+』program.ini』;
myinifile:=tinifile.create(filename);
四、讀取關鍵字的值
針對ini檔案支援的字串、整型數值、布林值三種資料型別,tinifiles類提供了三種不同的物件方法來讀取ini檔案中關鍵字的值。
假設已定義變數vs、vi、vb分別為string、 integer、boolean型別。
vs:=myinifile.readstring(』小節名』,』關鍵字』,預設值);
vi:=myinifile.readinteger(』小節名』,』關鍵字』,預設值);
vb:=myinifile.readbool(』小節名』,』關鍵字』,預設值);
其中預設值為該ini檔案不存在該關鍵字時返回的預設值。
五、寫入ini檔案
同樣的,tinifile類也提供了三種不同的物件方法,向ini檔案寫入字串、整型數及布林型別的關鍵字。
myinifile.writestring(』小節名』,』關鍵字』,變數或字串值);
myinifile.writeinteger(』小節名』,』關鍵字』,變數或整型數值);
myinifile.writebool(』小節名』,』關鍵字』,變數或true或false);
當這個ini檔案不存在時,上面的語句還會自動建立該ini檔案。
六、刪除關鍵字
除了可用寫入方法增加乙個關鍵字,tinifile類還提供了乙個刪除關鍵字的物件方法:
myinifile.deletekey(』小節名』,』關鍵字』);
七、小節操作
增加乙個小節可用寫入的方法來完成,刪除乙個小節可用下面的物件方法:
myinifile.erasesection(』小節名』);
另外tinifile類還提供了三種物件方法來對小節進行操作:
myinifile.readsection(』小節名』,tstrings變數);可將指定小節中的所有關鍵字名讀取至乙個字串列表變數中;
myinifile.readsections(tstrings變數);可將ini檔案中所有小節名讀取至乙個字串列表變數中去。
myinifile.readsectionvalues(』小節名』,tstrings變數);可將ini檔案中指定小節的所有行(包括關鍵字、=、值)讀取至乙個字串列表變數中去。
八、釋放
在適當的位置用下面的語句釋放myinifile:
myinifile.distory;
九、乙個例項
下面用乙個簡單的例子(如圖),演示了建立、讀取、存貯ini檔案的方法。myini.ini檔案中包含有「程式引數」小節,和使用者名稱(字串)、是否正式使用者(布林值)和已執行時間(整型值)三個關鍵字。程式在窗體建立讀取這些資料,並在窗體釋放時寫myini.ini檔案。
附源程式清單
unit unit1;
inte***ce
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs,inifiles, stdctrls, extctrls;
type
tform1 = class(tform)
edit1: tedit;
edit2: tedit;
checkbox1: tcheckbox;
timer1: ttimer;
procedure formcreate(sender: tobject);
procedure formdestroy(sender: tobject);
procedure timer1timer(sender: tobject);
private
public
end;
varform1: tform1;
myinifile:tinifile;
implementation
procedure tform1.formcreate(sender: tobject);
varfilename:string;
begin
filename:=extractfilepath(paramstr(0))+'myini.ini';
myinifile:=tinifile.create(filename);
edit1.text:= myinifile.readstring('程式引數','使用者名稱','預設的使用者名稱');
edit2.text:= inttostr(myinifile.readinteger('程式引數','已執行時間',0));
checkbox1.checked:= myinifile.readbool('程式引數','是否正式使用者',false);
end;
procedure tform1.formdestroy(sender: tobject);
begin
myinifile.writestring('程式引數','使用者名稱',edit1.text);
myinifile.writeinteger('程式引數','已執行時間',strtoint(edit2.text));
myinifile.writebool('程式引數','是否正式使用者',checkbox1.checked);
myinifile.destroy;
end;
procedure tform1.timer1timer(sender: tobject);
begin
edit2.text:=inttostr(strtoint(edit2.text)+1);
end;
end.
控制ini檔案幾法
要利用.ini檔案做程式有關資料的儲存工作,就需要能讀和寫.ini檔案,所以列了如下方法給大家參考:
從.ini檔案中獲取字串
varstrresult:pchar;
begin
getprivateprofilestring(
'windows', // 中標題的名字
'nullport', // =號前的名字
'nil', // 如果沒有找到字串時,返回的預設值
strresult, //存放取得字元
100, //取得字元的允許最大長度
'c:/forwin95/win.ini' // 呼叫的檔名
edit1.text:=strresult; //顯示取得字串
從.ini檔案中獲取整數
edit1.text:=inttostr(getprivateprofileint(
'intl', // 中標題的名字
'icountry', // =號前的名字
0,// 如果沒有找到整數時,返回的預設值
'c:/forwin95/win.ini' // 呼叫的檔名
向.ini檔案寫入字串
writeprivateprofilestring(
'windows', // 中標題的名字
'load', // 要寫入「=」號前的字串
'accca', //要寫入的資料
'c:/forwin95/win.ini' // 呼叫的檔名
向.ini檔案寫入整數
writeprivateprofilesection(
'windows', // 中標題的名字
'read=100', // 要寫入的資料
'c:/forwin95/win.ini' // 呼叫的檔名
上面的方法是呼叫api函式,下面介紹另一種不用api從.ini檔案中獲取字元的方法
var myini: tinifile;
begin
myini := tinifile.create('win.ini');//呼叫的檔名
edit1.text:=myini.readstring('desktop', 'wall*****', '');//取得字元
end;
向.ini檔案中寫入字元的方法
var myini: tinifile;
begin
myini := tinifile.create('win.ini');//呼叫的檔名
delphiini.writestring('desktop', 'wall*****', 'c:/a.bmp');
end;
DELPHI中對INI檔案的讀寫
function readinifile filename,section,ident string string 讀取配置檔案 varini tinifile begin ini tinifile.create filename result ini.readstring section,iden...
Delphi對ini檔案的操作
一 ini檔案的結構 注釋 小節名 關鍵字 值 ini檔案允許有多個小節,每個小節又允許有多個關鍵字,後面是該關鍵字的值。值的型別有三種 字串 整型數值和布林值。其中字串存貯在ini檔案中時沒有引號,布林真值用1表示,布林假值用0表示。注釋以分號 開頭。二 定義 1 在inte ce的uses節增加...
delphi對ini檔案的操作
定義 1 在inte ce的uses節增加inifiles 2 在var變數定義部分增加一行 myinifile tinifile 開啟ini檔案 myinifile tinifile.create program.ini 當地 filename extractfilepath paramstr 0...