INI 檔案仍是輕量級設定儲存的常見選擇。Delphi 透過 Windows API GetPrivateProfileString、GetPrivateProfileInt、WritePrivateProfileString、WritePrivateProfileSection 提供完整存取能力。本文整理標準呼叫模式與參數說明,可直接套用專案。
function GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize: DWORD; lpFileName: PChar): DWORD; stdcall;
function GetPrivateProfileInt(lpAppName, lpKeyName, nDefault: Integer; lpFileName: PChar): Integer; stdcall;
function WritePrivateProfileString(lpAppName, lpKeyName, lpString, lpFileName: PChar): BOOL; stdcall;
function WritePrivateProfileSection(lpAppName, lpString, lpFileName: PChar): BOOL; stdcall;
var
strResult: array[0..255] of Char; // 緩衝區
fileName: string;
begin
fileName := 'C:\MyApp\Settings.ini';
GetPrivateProfileString(
'Windows', // 區段名 [Windows]
'NullPort', // 鍵名 NullPort=
'NIL', // 找不到時的預設值
strResult, // 接收緩衝區
Length(strResult),// 緩衝區大小
PChar(fileName) // INI 完整路徑
);
Edit1.Text := StrPas(strResult); // 顯示結果
end;
var
intValue: Integer;
fileName: string;
begin
fileName := 'C:\MyApp\Settings.ini';
intValue := GetPrivateProfileInt(
'Intl', // 區段 [Intl]
'iCountry', // 鍵 iCountry=
0, // 預設值
PChar(fileName) // 路徑
);
Edit1.Text := IntToStr(intValue);
end;
var
fileName: string;
begin
fileName := 'C:\MyApp\Settings.ini';
WritePrivateProfileString(
'Windows', // 區段 [Windows]
'Load', // 鍵 Load=
'accca', // 寫入值
PChar(fileName)
);
end;
var
fileName: string;
begin
fileName := 'C:\MyApp\Settings.ini';
// 寫入整個區段內容,格式 "Key=Value" 以 #0 分隔,雙 #0 結尾
WritePrivateProfileSection(
'Windows',
'Read=100', // 實際建議用 "Read=100"#0#0
PChar(fileName)
);
end;
注意:WritePrivateProfileSection參數lpString需為「Key=Value」字串陣列,以#0分隔,雙#0結尾。單純寫入單一整數鍵值,建議改用WritePrivateProfileString搭配IntToStr。
unit IniHelper;
interface
uses SysUtils, Windows;
function ReadIniString(const Section, Key, Default, FileName: string): string;
function ReadIniInteger(const Section, Key: string; Default: Integer; const FileName: string): Integer;
procedure WriteIniString(const Section, Key, Value, FileName: string);
procedure WriteIniInteger(const Section, Key: string; Value: Integer; const FileName: string);
implementation
function ReadIniString(const Section, Key, Default, FileName: string): string;
var
Buffer: array[0..1023] of Char;
begin
GetPrivateProfileString(PChar(Section), PChar(Key), PChar(Default), Buffer, Length(Buffer), PChar(FileName));
Result := StrPas(Buffer);
end;
function ReadIniInteger(const Section, Key: string; Default: Integer; const FileName: string): Integer;
begin
Result := GetPrivateProfileInt(PChar(Section), PChar(Key), Default, PChar(FileName));
end;
procedure WriteIniString(const Section, Key, Value, FileName: string);
begin
WritePrivateProfileString(PChar(Section), PChar(Key), PChar(Value), PChar(FileName));
end;
procedure WriteIniInteger(const Section, Key: string; Value: Integer; const FileName: string);
begin
WritePrivateProfileString(PChar(Section), PChar(Key), PChar(IntToStr(Value)), PChar(FileName));
end;
end.
uses IniHelper;
var
iniFile: string;
begin
iniFile := ExtractFilePath(ParamStr(0)) + 'Config.ini';
// 寫入
WriteIniString('Database', 'Server', '192.168.1.100', iniFile);
WriteIniInteger('Database', 'Port', 5432, iniFile);
// 讀取
EditServer.Text := ReadIniString('Database', 'Server', 'localhost', iniFile);
EditPort.Text := IntToStr(ReadIniInteger('Database', 'Port', 5432, iniFile));
end;
ExtractFilePath(ParamStr(0)) 取得執行檔目錄,避免硬編碼路徑。Program Files 寫入,設定檔應置於 AppData 或執行檔同目錄。TIniFile/TMemIniFile (VCL 封裝類),支援快取與批次寫入。掌握 Windows API 直接操作 INI 檔案,可在無 VCL 依賴(如 DLL、服務程式)環境下輕鬆實現設定持久化。封裝為通用單元後,專案中即可一行程式碼完成讀寫,簡潔且可維護。