如何获得WINDOWS未知类型的文件的大小? (100分)

  • 主题发起人 主题发起人 lee_xl
  • 开始时间 开始时间
L

lee_xl

Unregistered / Unconfirmed
GUEST, unregistred user!
例如:<br>var<br>&nbsp; Security: TSecurityAttributes; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; hFile: Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; FileInfo: TByHandleFileInformation; &nbsp; <br>&nbsp; Intermediate: TFileTime; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; SystemTime: TSystemTime; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br><br>&nbsp; FileType: DWORD; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>begin<br>&nbsp; Security.nLength:=SizeOf(TSecurityAttributes);<br>&nbsp; Security.bInheritHandle:=FALSE;<br>&nbsp; hFile:=CreateFile(PChar(FileListBox1.FileName), GENERIC_READ, 0, @Security,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);<br>&nbsp; if (hFile &lt;&gt; INVALID_HANDLE_VALUE) then<br>&nbsp; begin<br>&nbsp; &nbsp; GetFileInformationByHandle(hFile,FileInfo);<br>&nbsp; &nbsp; Label1.Caption := ExtractFileName('SQLServer200DataBase.mdf');<br>&nbsp; &nbsp; FileTimeToLocalFileTime(FileInfo.ftLastWriteTime, Intermediate);<br>&nbsp; &nbsp;FileTimeToSystemTime(Intermediate, SystemTime);<br>&nbsp;<br>&nbsp; &nbsp;{display the volume serial number}<br>&nbsp; &nbsp; Label8.Caption:=IntToStr(FileInfo.dwVolumeSerialNumber);<br>&nbsp; &nbsp; {display the file size}<br>&nbsp; &nbsp; Label4.Caption:=IntToStr(GetFileSize(hFile, nil))+ ' bytes';<br>&nbsp; &nbsp; FileType:=GetFileType(hFile);<br>&nbsp; <br>&nbsp; &nbsp; CloseHandle(hFile);<br>&nbsp; end;<br>但是windows判断不出文件类型,得出的文件大小为0!!!<br>请大家赐教!<br>
 
可以这样做:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; fs: TFileStream;<br>begin<br>&nbsp; fs:= TFileStream.Create('c:/temp/test.123',fmOpenRead);<br>&nbsp; try<br>&nbsp; &nbsp; ShowMessage(IntToStr(fs.Size)+' Bytes');<br>&nbsp; finally<br>&nbsp; &nbsp; fs.Free;<br>&nbsp; end;<br>end;
 
不灵!<br>提示不能打开sql server的数据库文件,即*.mdf格式的
 
这样就可以了,不过方法不是特别好。其实得到文件大小的方法很多。<br>后面列了一个摘自HUbdog的葵花宝典的例子,得到目录所有文件信息,当然可以摘捡一下。<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; fs: TFileStream;<br>begin<br>&nbsp; fs := TFileStream.Create('C:/1.doc', fmOpenRead or fmShareDenyNone);<br>&nbsp; try<br>&nbsp; &nbsp; ShowMessage(IntToStr(fs.Size) + ' Bytes');<br>&nbsp; finally<br>&nbsp; &nbsp; fs.Free;<br>&nbsp; end;<br>end;<br>--------------------------------------<br>获取目录下全部文件大小<br>function GetDirectorySize(const ADirectory: string): Integer;<br>var<br>&nbsp; Dir: TSearchRec;<br>&nbsp; Ret: integer;<br>&nbsp; Path: string;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; Path := ExtractFilePath(ADirectory);<br>&nbsp; Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir);<br>&nbsp; if Ret = NO_ERROR then<br>&nbsp; &nbsp; exit;<br>&nbsp; try<br>&nbsp; &nbsp; while ret = NO_ERROR do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; inc(Result, Dir.Size);<br>&nbsp; &nbsp; &nbsp; if (Dir.Attr in [faDirectory]) and (Dir.Name[1]?? '.') then<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc(Result, GetDirectorySize(Path + Dir.Name + '/*.*'));<br>&nbsp; &nbsp; &nbsp; Ret := Sysutils.FindNext(Dir);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; Sysutils.FindClose(Dir);<br>&nbsp; end;<br>end;<br>
 
Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir);<br>&nbsp; if Ret = NO_ERROR then<br>&nbsp; &nbsp; exit;<br>&nbsp; try<br>&nbsp; &nbsp; while ret = NO_ERROR do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; inc(Result, Dir.Size);<br>&nbsp; &nbsp; &nbsp; if (Dir.Attr in [faDirectory]) and (Dir.Name[1]?? '.') then<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc(Result, GetDirectorySize(Path + Dir.Name + '/*.*'));<br>&nbsp; &nbsp; &nbsp; Ret := Sysutils.FindNext(Dir);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; Sysutils.FindClose(Dir);<br>&nbsp; end;<br>
 
后退
顶部