以下為小白個人使用的 INIUnit
(* ---------------------------------------------------------------------------------------------- *)
// XBINIUnit_FMX
// Author: Kamilia
// 用途:控制INI
// 初版: 2016/09/24
(* ---------------------------------------------------------------------------------------------- *)
unit XBINIUnit_FMX;
interface
uses classes, SysUtils, inifiles, System.IOUtils, FMX.Forms, FMX.dialogs;
// 執行檔所在資料夾
Function getNDir: string;
// 判斷INI檔案存在
procedure CheckINI(Filename: string);
// 主要INI路徑
Function Mainini: string;
// 取得 MainIni Value;
Function GetMIValue(VName: string; DefValue: string = ''): string;
// 設定 MainIni Value;
Procedure SetMIValue(VName, Value: string);
implementation
(* ---------------------------------------------------------------------------------------------- *)
// getNDir 執行檔所在資料夾
(* ---------------------------------------------------------------------------------------------- *)
Function getNDir: string;
begin
{$IFDEF MSWINDOWS}
result := ExtractFileDir(ParamStr(0));
{$ENDIF MSWINDOWS}
{$IFDEF ANDROID}
result := TPath.GetDocumentsPath;
{$ENDIF}
// PathDelim >> windows = "/" esle "\"
result := result + PathDelim;
end;
(* ---------------------------------------------------------------------------------------------- *)
// CheckINI 判斷INI檔案存在
(* ---------------------------------------------------------------------------------------------- *)
procedure CheckINI(Filename: string);
var
ts: tstrings;
begin
if fileexists(Filename) then
Exit;
// ForceDirectories(ExtractFileDir(Filename)); //返回 Boolean
if not DirectoryExists(ExtractFileDir(Filename)) then
CreateDir(ExtractFileDir(Filename));
ts := tstringList.Create;
ts.savetofile(Filename);
ts.free;
end;
(* ---------------------------------------------------------------------------------------------- *)
// Mainini 主要INI路徑
(* ---------------------------------------------------------------------------------------------- *)
Function Mainini: string;
Begin
result := getNDir + 'setup.ini';
End;
(* ---------------------------------------------------------------------------------------------- *)
// getMIValue 取得 MainIni Value;
(* ---------------------------------------------------------------------------------------------- *)
Function GetMIValue(VName: string; DefValue: string = ''): string;
var
ts: tstrings;
begin
CheckINI(Mainini);
ts := tstringList.Create;
ts.LoadFromFile(Mainini);
if ts.Values[VName] = '' then
begin
ts.Values[VName] := DefValue;
ts.savetofile(Mainini);
end;
result := ts.Values[VName];
ts.free;
end;
(* ---------------------------------------------------------------------------------------------- *)
// SetMIValue 設定 MainIni Value;
(* ---------------------------------------------------------------------------------------------- *)
Procedure SetMIValue(VName, Value: string);
var
ts: tstrings;
begin
CheckINI(Mainini);
ts := tstringList.Create;
ts.LoadFromFile(Mainini);
ts.Values[VName] := Value;
ts.savetofile(Mainini);
ts.free;
end;
end.
附上檔案
https://drive.google.com/open?id=0B_Boa0V6iHIuN294Zzl4aUxRTzQ
歡迎引用,但請勿修改,並保留出處