轻松获取应用程序exe大小
Delphi中有没有一种方法可以通过一两行代码获取当前应用程序的exe大小?
Is there a way in Delphi to get the currect application's exe size in one or two lines of code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
只是为了笑……你也可以用流来做到这一点,只需略多于 2 行代码。 通常,应用程序文件名(包括路径)也存储到 Paramstr(0) 中。
Just for grins...you can also do this with streams Just slightly more than 2 lines of code. Generally the application filename including path is also stored into Paramstr(0).
它没有你想要的那么小,但它不需要把手。 我在所有必须知道其大小的“SFX”存档器和程序中使用它。 IIRC 它需要 Windows 设备。
It's not as small as you want, but it needs no handles. I use this in all my "SFX" archivers and programs that must know their size. IIRC it requires the Windows unit.
为了将来的兼容性,您应该尽可能选择不需要指针或 Windows API 函数的实现。 skamradt 提供的基于 TFileStream 的解决方案对我来说看起来不错。
但是...您不必太担心该例程是 1 行代码还是 10 行代码,因为无论如何您都会将其封装在一个函数中,该函数以文件名作为参数并返回 Int64,并将其放入您的个人可重用代码库。 然后你可以像这样调用它:
GetMyFileSize(Application.ExeName);
For the sake of future compatibility, you should choose an implementation that does not require pointers or Windows API functions when possible. The TFileStream based solution provided by skamradt looks good to me.
But... You shouldn't worry too much whether the routine is 1 or 10 lines of code, because you're going to encapsulate it anyway in a function that takes a filename as a parameter and returns an Int64, and put it in your personal library of reusable code. Then you can call it like so:
GetMyFileSize(Application.ExeName);
你可以试试这个:
===============
内夫塔利
You can try this:
===============
Neftalí
流也可以在没有 TFileStream 变量的情况下使用:
丑陋,是的。
我更喜欢使用 DSiWin32 中的 DSiFileSize。 它在内部使用 CreateFile:
Streams can also be used without a TFileStream variable:
Ugly, yes.
I prefer using DSiFileSize from DSiWin32. It uses CreateFile internally:
不幸的是,如果不使用某些库,仅用一两行代码就不可能做到这一点。
最简单的部分是获取应用程序的 exe 文件。 您可以在
Application.ExeName
中找到它。一般来说,检索文件大小有多种可能性:
FileOpen
和FileSize
或使用TFileStream
(使用size
属性)或使用 Win32 API 函数CreateFile
和GetFileSize
函数。 (取决于平台!)确保以只读访问权限打开文件。TSearchRec.FindData.nFileSizeLow
读取它。 如果您想为大于 2 GB 的文件做好准备(您应该这样做),您还必须使用nFileSizeHigh
部分。System.IO.FileInfo
,如下所示:FileInfo.Create(filename).Length
(单行)lstat64
函数(UnitLibc
)并从TStatBuf64.st_size
获取大小。 (如果不计算变量声明,则为两行)在 JCL 库 中,您可以找到许多有用的函数,包括一个返回给定文件名的文件大小的简单函数。 (它使用适合给定平台的方法)
Unfortunatly it is not possible to do that with only one or two lines of code without using some library.
The easy part is getting the application's exe file. You can find it in
Application.ExeName
In general there are several possibilities for retrieving the file size:
FileOpen
andFileSize
, or withTFileStream
(use thesize
property) or with Win32 API functionsCreateFile
andGetFileSize
function. (Platform dependend!) Make sure you open the file with read-only access.FindFirst
to get the file size. You can read it fromTSearchRec.FindData.nFileSizeLow
. If you want to be prepared for files larger than 2 GB (you should be) you have to use also thenFileSizeHigh
part.System.IO.FileInfo
, like this:FileInfo.Create(filename).Length
(one-liner)lstat64
function (UnitLibc
) and get the size fromTStatBuf64.st_size
. (two-liner if you don't count the variable declaration)In the JCL library you can find many useful functions, including a simple function which returns the file size of a given file name. (It uses a method which suits the given platform)
我想修改 skamradt 提供的代码,使其成为您要求的两行代码;-)
但我更愿意使用 skamradt 编写的代码,因为它更安全
I would like to modify the code provided by skamradt, to make it two lines of code as you requested ;-)
but I would prefer to use the code as skamradt wrote, because it's more safe
我能做的最短的。 请注意,.Size 以字节为单位,因此对于千字节,请除以 1024。
查看 这个链接。
Shortest I could do. Note that the .Size is in bytes, so for kilobytes, divide by 1024.
Check out this link.