讀取TIniFile的速度太慢 |
答題得分者是:h@visli
|
johnny2212
初階會員 發表:34 回覆:65 積分:39 註冊:2003-04-09 發送簡訊給我 |
|
h@visli
資深會員 發表:103 回覆:429 積分:431 註冊:2004-02-13 發送簡訊給我 |
1、INIFile適合保存一些簡單的配置,當存儲的內容較多,體積變大後,讀取的速度是會慢。
2、您也可以考慮采用寫到註冊表中或XML文件中。
3、您要保存元件的各種特性,可以采用下面的方法:
This example shows how to use the built-in component streaming support to convert any component into a string and convert that string back into a component. function ComponentToString(Component: TComponent): string; var BinStream:TMemoryStream; StrStream: TStringStream; s: string; begin BinStream := TMemoryStream.Create; try StrStream := TStringStream.Create(s); try BinStream.WriteComponent(Component); BinStream.Seek(0, soFromBeginning); ObjectBinaryToText(BinStream, StrStream); StrStream.Seek(0, soFromBeginning); Result:= StrStream.DataString; finally StrStream.Free; end; finally BinStream.Free end; end; function StringToComponent(Value: string): TComponent; var StrStream:TStringStream; BinStream: TMemoryStream; begin StrStream := TStringStream.Create(Value); try BinStream := TMemoryStream.Create; try ObjectTextToBinary(StrStream, BinStream); BinStream.Seek(0, soFromBeginning); Result := BinStream.ReadComponent(nil); finally BinStream.Free; end; finally StrStream.Free; end; end;或者采用 ReadComponentResFile, WriteComponentResFile 方法: The following code defines a component class (TStreamable class) and a persistent class (TContainedClass) that is the type of a property on the component class. When a button is clicked, an instance of the component class is created, saved to a file, deleted, and then loaded from the file. By setting a breakpoint on the property setter of the TContainedClass SomeData property, you can watch how the streaming system sets property values when it reads them from a file. The following unit defines the classes to stream: unit StrmExmpl; {$R-,T-,H ,X } interface uses Classes type TContainedClass = class(TPersistent) private: FSomeData: Integer; procedure SetSomeData(Value: Integer); public: constructor Create; override; // Only published properties // are automatically streamed. published: property SomeData: Integer read FSomeData write SetSomeData; end; TStreamableClass = class(TComponent) private: FContainedClass: TContainedClass; public: constructor Create(AOwner: TComponent); override; destructor Destroy; override; // Only published properties // are automatically streamed. published: property ContainedClass: TContainedClass read FContainedClass write FContainedClass; end; implementation procedure TContainedClass.SetSomeData(Value: Integer); begin { Place a breakpoint here and notice how the data is streamed back. } FSomeData := Value; end; constructor TContainedClass.Create; begin FSomeData := 42; end; constructor TStreamableClass.Create(AOwner: TComponent); begin inherited Create(AOwner); FContainedClass = TContainedClass.Create; end; destructor TStreamableClass.Destroy; begin FContainedClass.Free; end; initialization RegisterClasses([TContainedClass, TStreamableClass]); finalization end. The following OnClick event handler should be added to a button on the application抯 main form. It causes the above classes to be streamed out and in. Note that you must add StrmExmpl to the uses clause of unit1. procedure TForm1.Button1Click(Sender: TObject); var AClassInstance: TStreamableClass; begin AClassInstance := TStreamableClass.Create(nil); WriteComponentResFile('C:\Test', AClassInstance); FreeAndNil(AClassInstance); AClassInstance := ReadComponentResFile('C:\Test', nil) as TStreamableClass; FreeAndNil(AClassInstance); end;---------------------------- 於Delphi K.Top之上 博采眾家之長, 奉獻綿薄之力 --------------------------- 發表人 - h@visli 於 2005/09/21 17:24:48 發表人 - h@visli 於 2005/09/21 17:27:37
------
------------------------ 博采眾家之長,奉獻綿薄之力 ------------------------ |
RedSnow
版主 發表:79 回覆:1322 積分:845 註冊:2003-12-15 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |