function GetContext(const AIniFilename: string): string;
var
FIniFile: TIniFile;
Context_Main, Context_Function: string;
begin
if not FileExists(AIniFilename) then exit;
FIniFile:= TIniFile.Create(AIniFilename);
try
Context_Main:= FIniFile.ReadString('Context', 'Main', '');
Context_Function:= FIniFile.ReadString('Context', 'Function', '');
Result:= format('%s%s', [Context_Main, Context_Function]);
finally
FIniFile.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Filename: string;
begin
Filename:= 'Context.ini';
Edit1.Text:= GetContext(Filename);
end;
ini內容:
[Context]
Main=123456
Function=ABCDEFG
Procedure=hello
Function=wxyz
Procedure=World
更完整的範例:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,IniFiles,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
AIniFileName = 'funds.ini';
implementation
{$R *.dfm}
//讀取資訊
procedure TForm1.Button1Click(Sender: TObject);
var
FIniFile: TIniFile;
begin
//if not FileExists(AIniFilename) then exit;
FIniFile:= TIniFile.Create(AIniFilename);
try
memo1.Lines.add(FIniFile.ReadString('基金顯示資訊','國內或國外基金', ''));
memo1.Lines.add(FIniFile.ReadString('基金顯示資訊','基金公司', ''));
memo1.Lines.add(FIniFile.ReadString('基金顯示資訊','基金名稱', ''));
memo1.Lines.add(FIniFile.ReadString('基金顯示資訊','指標數目', ''));
finally
FIniFile.Free;
end;
end;
//寫入ini
procedure TForm1.Button2Click(Sender: TObject);
var
FIniFile: TIniFile;
begin
//if not FileExists(AIniFilename) then exit;
FIniFile:= TIniFile.Create(AIniFilename);
try
FIniFile.WriteString('基金顯示資訊','指標數目', '0');
FIniFile.WriteString('指標1','指標名稱', 'MACD');
FIniFile.WriteString('指標1','參數1', '5');
FIniFile.WriteString('指標1','參數2', '3');
FIniFile.WriteString('指標1','參數3', '3');
FIniFile.WriteString('指標1','參數4', '3');
finally
FIniFile.Free;
end;
end;
//刪除
procedure TForm1.Button3Click(Sender: TObject);
var
FIniFile: TIniFile;
begin
FIniFile:= TIniFile.Create(AIniFilename);
try
FIniFile.EraseSection('基金顯示資訊');
FIniFile.DeleteKey('指標1','指標名稱');
finally
FIniFile.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
FIniFile: TIniFile;
begin
if not FileExists(AIniFilename) then //可以放在temp目錄內好了
begin
FIniFile:= TIniFile.Create(AiniFileName);
try
FIniFile.WriteString('基金顯示資訊','國內或國外基金', '國內基金');
FIniFile.WriteString('基金顯示資訊','基金公司', '富達證券');
FIniFile.WriteString('基金顯示資訊','基金名稱', '富達新興市場基金');
FIniFile.WriteString('基金顯示資訊','指標數目', '0');
FIniFile.WriteString('指標1','指標名稱', '');
FIniFile.WriteString('指標1','參數1', '');
FIniFile.WriteString('指標1','參數2', '');
FIniFile.WriteString('指標1','參數3', '');
FIniFile.WriteString('指標1','參數4', '');
FIniFile.WriteString('指標2','指標名稱', '');
FIniFile.WriteString('指標2','參數1', '');
FIniFile.WriteString('指標2','參數2', '');
FIniFile.WriteString('指標2','參數3', '');
FIniFile.WriteString('指標2','參數4', '');
FIniFile.WriteString('指標3','指標名稱', '');
FIniFile.WriteString('指標3','參數1', '');
FIniFile.WriteString('指標3','參數2', '');
FIniFile.WriteString('指標3','參數3', '');
FIniFile.WriteString('指標3','參數4', '');
finally
FIniFile.Free;
end;
end;
end;
end.
沒有留言:
張貼留言