Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > API de Windows
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

 
 
Herramientas Buscar en Tema Desplegado
  #4  
Antiguo 23-09-2014
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Poder: 23
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
jars,

Cita:
Empezado por jars
...estoy usando esta función para obtener el tamaño de un archivo desde un programa que corre como servicio...un cliente que lo esta ejecutando en una maquina virtual con Windows Server 2008 SP2...en lugar de devolverme el tamaño me esta devolviendo una fecha...


Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  function GetFileSizeEx(hFile: THandle; var FSize: Int64): boolean; stdcall;
                         external 'Kernel32.dll' name 'GetFileSizeEx';

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetFileSize1(FileName : String) : Int64;
var
   F : File of Byte;
begin
   try
      FileMode := fmOpenRead;
      AssignFile(F, FileName);
      Reset(F);
      Result := FileSize(F);
      CloseFile(F);
   except
      Result := -1;
   end;
end;

function GetFileSize2(FileName : String) : Int64;
var
   F : TFileStream;
begin
   try
      F := TFileStream.Create(FileName,fmOpenRead,fmShareDenyNone);
      Result := F.Size;
      F.Free;
   except
      Result := -1;
   end;
end;

function GetFileSize3(const FileName: string): Int64;
var
   F: THandle;
begin
   try
      F := CreateFile(PChar(FileName), 0, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
      if F = INVALID_HANDLE_VALUE then
      begin
         Result := -1;
         Exit;
      end;
      Result := GetFileSize(F,nil);
      CloseHandle(F);
   except
      Result := -1;
   end;
end;

function GetFileSize4(const FileName: string): Int64;
var
   SR : TSearchRec;
begin
   try
      if FindFirst(FileName, faAnyFile, SR ) <> 0 then
      begin
         Result := -1;
         Exit;
      end;
      Result := SR.Size;
      FindClose(SR);
   except
      Result := -1;
   end;
end;

function GetFileSize5(const FileName: string): Int64;
var
   F : THandle;
begin
   try
      F := FileOpen(FileName, fmOpenRead);
      Result := FileSeek(F,0,2);
      FileClose(F);
   except
      Result := -1;
   end;
end;

function GetFileSize6(const FileName: string): Int64;
var
   F : THandle;
begin
   try
      F := FileOpen(FileName, fmOpenRead);
      if not GetFileSizeEx(F, Result) then
         Result:= -1;
      FileClose(F);
   except
      Result := -1;
   end;
end;

function GetFileSize7(const FileName: string): Int64;
var
   FindData : TWin32FindData;
begin
   Windows.FindClose(FindFirstFile(PChar(FileName), FindData));
   Result := FindData.nFileSizeHigh shl 32 or FindData.nFileSizeLow;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   openDialog : TOpenDialog;
   FileName : String;
   MsgUser : String;

begin

   openDialog := TOpenDialog.Create(self);
   openDialog.InitialDir := GetCurrentDir;
   openDialog.Options := [ofFileMustExist];
   openDialog.Filter := 'Files |*.*';

   if openDialog.Execute then
   begin
      FileName := openDialog.FileName;

      MsgUser := Format('GetFileSize1 : %s, Size = %d',[FileName, GetFileSize1(FileName)]);
      MessageDlg(MsgUser,mtInformation,[mbOK],0);

      MsgUser := Format('GetFileSize2 : %s, Size = %d',[FileName, GetFileSize2(FileName)]);
      MessageDlg(MsgUser,mtInformation,[mbOK],0);

      MsgUser := Format('GetFileSize3 : %s, Size = %d',[FileName, GetFileSize3(FileName)]);
      MessageDlg(MsgUser,mtInformation,[mbOK],0);

      MsgUser := Format('GetFileSize4 : %s, Size = %d',[FileName, GetFileSize4(FileName)]);
      MessageDlg(MsgUser,mtInformation,[mbOK],0);

      MsgUser := Format('GetFileSize5 : %s, Size = %d',[FileName, GetFileSize5(FileName)]);
      MessageDlg(MsgUser,mtInformation,[mbOK],0);

      MsgUser := Format('GetFileSize6 : %s, Size = %d',[FileName, GetFileSize6(FileName)]);
      MessageDlg(MsgUser,mtInformation,[mbOK],0);

      MsgUser := Format('GetFileSize7 : %s, Size = %d',[FileName, GetFileSize7(FileName)]);
      MessageDlg(MsgUser,mtInformation,[mbOK],0);
   end
   else
   begin
      MsgUser := 'No se Selecciono Ningún Archivo';
      MessageDlg(MsgUser,mtInformation,[mbOK],0);
   end;

   openDialog.Free;

end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32, obtiene el tamaño de un archivo por diferentes métodos.

Nota: El código fue probado en los siguientes entornos

1- Una máquina física con Windows 7 Profesional x32.

2- Una VMware con Windows 8.1 Professional x32.

3- Una VMware con Windows 7 Professional x64.

En todos los casos de prueba, el código del ejemplo funciono correctamente según lo esperado.

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 23-09-2014 a las 22:05:11.
Responder Con Cita
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Obtener tamaño de archivo con error jars API de Windows 2 20-05-2013 21:15:19
Parámetro VarChar de búsqueda, ¿es válido aumentar su tamaño para evitar error? Al González Conexión con bases de datos 15 18-11-2008 22:16:35
Como obtener el tamaño de un archivo en delphi 7 kurono Varios 2 02-10-2007 09:58:21
Como Obtener tamaño de un Archivo onlytk Varios 2 20-11-2006 23:06:15
Obtener el Tamaño de un Archivo Viet OOP 1 09-09-2003 14:34:03


La franja horaria es GMT +2. Ahora son las 08:04:29.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi