Ros*_*erg 3 delphi delphi-7 delphi-xe
说我有一个本地文件:
C:\ file.txt的
一个在网上:
如何检查尺寸是否不同,如果它们不同则//做一些事情?
谢谢.
要获取Internet上文件的文件大小,请执行此操作
function WebFileSize(const UserAgent, URL: string): cardinal;
var
hInet, hURL: HINTERNET;
len: cardinal;
index: cardinal;
begin
result := cardinal(-1);
hInet := InternetOpen(PChar(UserAgent),
INTERNET_OPEN_TYPE_PRECONFIG,
nil,
nil,
0);
index := 0;
if hInet <> nil then
try
hURL := InternetOpenUrl(hInet, PChar(URL), nil, 0, 0, 0);
if hURL <> nil then
try
len := sizeof(result);
if not HttpQueryInfo(hURL,
HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER,
@result,
len,
index) then
RaiseLastOSError;
finally
InternetCloseHandle(hURL);
end;
finally
InternetCloseHandle(hInet)
end;
end;
Run Code Online (Sandbox Code Playgroud)
例如,你可以试试
ShowMessage(IntToStr(WebFileSize('Test Agent',
'http://privat.rejbrand.se/test.txt')));
Run Code Online (Sandbox Code Playgroud)
要获得本地文件的大小,最简单的方法是FindFirstFile在其上阅读TSearchRec.但是,稍微优雅一点
function GetFileSize(const FileName: string): cardinal;
var
f: HFILE;
begin
result := cardinal(-1);
f := CreateFile(PChar(FileName),
GENERIC_READ,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if f <> 0 then
try
result := Windows.GetFileSize(f, nil);
finally
CloseHandle(f);
end;
end;
Run Code Online (Sandbox Code Playgroud)
现在你可以做到
if GetFileSize('C:\Users\Andreas Rejbrand\Desktop\test.txt') =
WebFileSize('UA', 'http://privat.rejbrand.se/test.txt') then
ShowMessage('The two files have the same size.')
else
ShowMessage('The two files are not of the same size.')
Run Code Online (Sandbox Code Playgroud)
注意:如果在您的情况下使用32位来表示文件大小是不够的,您需要对上面的两个函数进行一些小的更改.
| 归档时间: |
|
| 查看次数: |
847 次 |
| 最近记录: |