如何在运行时将资源提取到文件中? [英] How can I extract a resource into a file at runtime?

查看:206
本文介绍了如何在运行时将资源提取到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想分发一个.exe,但是在运行时,我希望将某些嵌入的图像资源提取给用户的硬盘驱动器。

I want to distribute only a single .exe, however, at runtime I would like it to extract some embedded image resources to the users hard disk drive.

可以我,如果是,怎么样?

Can I, and if so, how?

推荐答案

使用Delphi的TResourceStream。它的构造函数会将资源找到并加载到内存中,而SaveToFile方法将会执行磁盘写入。

Use Delphi's TResourceStream. It's constructor will find and load the resource into memory, and it's SaveToFile method will do the disk write.

与此类似的内容应该起作用:

Something similar to this should work:

var
  ResStream: TResourceStream;
begin
  ResStream := TResourceStream.Create(HInstance, 'YOURRESOURCENAME', RT_RCDATA);
  try
    ResStream.Position := 0;
    ResStream.SaveToFile('C:\YourDir\YourFileName.jpg');
  finally
    ResStream.Free;
  end;
end;

如果可以使用资源ID而不是名称,那么它的内存要少一些。在这种情况下,您可以使用CreateFromID重新创建,并提供数字ID而不是字符串名称。

If you can use the resource ID instead of name, it's a little less memory. In that case, you'd resplace Create with CreateFromID, and supply the numeric ID rather than the string name.

这篇关于如何在运行时将资源提取到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆