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

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

问题描述

要确定我的exe路径,我不知道应该使用哪个代码。
请给我解释什么时候和为什么应该使用:



1。 ExtractFilePath(ParamStr ))



2。 ExtractFilePath(Application.ExeName)



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

  C:\Users\Bianca\Documents\RAD Studio\Projects\Exam1\Win32\Release\ 
C:\Users\Bianca\Documents\RAD Studio\ Projects \Exam1\Win32\Release\


解决方案

他们都给你相同的结果,但有微妙的区别。



Application.ExeName引用VCL Application对象。使用此属性需要使用Vcl.Forms单元。在内部,它确实调用ParamStr(0)函数。



请注意,FireMonkey TApplication类没有此属性(从XE5开始)。所以如果你正在使用FireMonkey,你不能调用Application.ExeName。如果您将VCL项目迁移到FireMonkey,您将不得不重写。



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



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

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:\Users\Bianca\Documents\RAD Studio\Projects\Exam1\Win32\Release\
C:\Users\Bianca\Documents\RAD Studio\Projects\Exam1\Win32\Release\

解决方案

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

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.

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.

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)

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

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

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