1.是不是這樣?
var
s: tstringlist;
begin
s := tstringlist.create;
s.add('aaaaaaaaa');
s.add('bbbbbbb');
s.savetofile('c:/a.txt')
/ /这样就可以保存
s.free;
end;
2。还是这样?使用下面的两个函数转换控件为TEXT,或还原。不过tstringlist是从tstrings-->tpersistent不属性TCOMPONENT,你可以建立一个类里面,再公布(published)一個TSTRINGLIST,然後保存這個類。
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;