Delphi 获取文件位置 [英] Delphi Get file location

查看:28
本文介绍了Delphi 获取文件位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要确定我的exe路径,我不知道应该使用哪个代码.请给我解释我们何时以及为什么应该使用:

To determine my exe path, I don't know which code I should use. Please give me explanation when and why should we use:

1. ExtractFilePath(ParamStr(0))

2. ExtractFilePath(Application.ExeName)

因为两个代码都呈现相同的输出.

Because both code are rendering the same output.

C:UsersBiancaDocumentsRAD StudioProjectsExam1Win32Release
C:UsersBiancaDocumentsRAD StudioProjectsExam1Win32Release

推荐答案

它们都给你相同的结果,但有细微的差别.

They both give you the same result, but there are subtle differences.

Application.ExeName 引用 VCL 应用程序对象.使用此属性需要您使用 Vcl.Forms 单元.这在内部确实调用了 ParamStr(0) 函数.

Application.ExeName references the VCL Application object. The use of this property requires you use the Vcl.Forms unit. Internally this does call the ParamStr(0) function.

注意 FireMonkey TApplication 类没有这个属性(从 XE5 开始).因此,如果您使用 FireMonkey,则不能调用 Application.ExeName.如果您曾经将 VCL 项目迁移到 FireMonkey,您将不得不重写它.

Notice that the FireMonkey TApplication class does not have this property (as of XE5). So you cannot call Application.ExeName if you are using FireMonkey. And if you ever migrate a VCL project to FireMonkey you will have to rewrite this.

ParamStr 函数 OTOH 是系统单元,并且是多平台的(Win、Mac、iOS 和 Android,取决于您使用的 Delphi 版本的 OC).在 Windows ParamStr(0) 上调用 GetModuleFileName 函数,而在其他平台上它解析命令行,返回第一个标记,它应该是运行的可执行文件的完整路径和名称.(感谢 Rob Kennedy 的更正)

The ParamStr function OTOH is the System unit and is multiplatform (Win, Mac, iOS and Android, depending OC on the Delphi version you are using). On Windows ParamStr(0) calls the GetModuleFileName function, while on the other platforms it parses the command line returning the first token, which should be full path and name of running executable. (Thanks to Rob Kennedy for this correction)

所以...我建议你直接使用 ParamStr(0).

So... I'd suggest you use ParamStr(0) directly.

这篇关于Delphi 获取文件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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