本文整理自网络,侵删。
准备好你的文本文件
text.txt${Name} 路过
新建一个扩展名为".rc"的文件(resource),内容为:
test.rcTest_Text File "test.txt"
格式为:资源名 类型 文件名(没加路径就是当前路径)
工程里添加该".rc"文件
Delphi菜单->Project->Add to Project... (文件类型选为“*.rc”)
上面的步骤完成后,可以在工程代码中看到:
Delphi(Pascal) code
program Project1;
{$R 'test.res' 'test.rc'}
//...
文本资源已经加到当前工程中
如下代码就不再加说明了,你自己查查帮助看看
Delphi(Pascal) code
procedure TForm1.Button1Click(Sender: TObject);
var
S: string;
begin
with TResourceStream.Create(HInstance, 'Test_Text', 'File') do try
if Size <= 0 then Exit;
SetLength(S, Size);
Read(S[1], Size);
with TStringList.Create do try
Text := StringReplace(S, '${Name}', 'Zswang',
[rfReplaceAll, rfIgnoreCase]); // 替换字符
SaveToFile('c:\temp\temp.txt'); // 保存为文件
finally
Free;
end;
finally
Free;
end;
end;
输出
c:\temp\temp.txt
相关阅读 >>
更多相关阅读请进入《Delphi》频道 >>