Delphi 透過 GetPrivateProfileString、GetPrivateProfileInt、WritePrivateProfileString、WritePrivateProfileSection 操作 INI 檔,需在 uses 子句引入 Windows 單元。
var
strResult: PChar;
begin
GetPrivateProfileString(
'windows', // 區段名
'NullPort', // 鍵名
'NIL', // 預設值
strResult, // 接收緩衝區
100, // 緩衝區大小
'c:\forwin95\win.ini' // INI 路徑
);
Edit1.Text := strResult;
end;
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', // 完整 "Key=Value" 字串
'c:\forwin95\win.ini' // INI 路徑
);
GetPrivateProfileString 的緩衝區需預先配置足夠大小(如 SetLength(strResult, 255))。Key=Value 字串,讀取時仍用 GetPrivateProfileInt 自動轉型。TIniFile / TMemIniFile 封裝類,操作更物件導向、支援 Unicode,新專案建議優先使用。