Página 1 de 1

[Delphi]ArchivoToString - Lite By ØnLy

Publicado: 07 Dic 2010, 22:33
por ØnLy
Bueno m,e hice esta funcion para cargar la data de un arhcivo en un string , sin
recurir a la pesada Clase TFileStream , esta es full WinAPI y muy liviana!

Autor : ØnLy
Agradecimientos : winapi.conclase.net
Lenguaje: Pascal/Delphi
Descripcion: Carga un archivo binario en un string( RunTime)

Código: Seleccionar todo

Uses
 Windows;

function ArchivoToString( path: string) : string;
var
Archivo : THandle;
talla , bytesleidos : Cardinal;
buffer : String;
begin
Archivo := CreateFile(PChar(path),GENERIC_READ, FILE_SHARE_READ,nil, OPEN_EXISTING,0,0);  //Abrimos e archivo para ectura
if Archivo <> INVALID_HANDLE_VALUE then begin
   //Obtenemos el tamano del archhivio
  talla :=  GetFileSize( Archivo , nil );
  SetLength( buffer , talla); //Le ponemos al string la dmensn del archivo
  ReadFile(Archivo , buffer[1] , talla , bytesleidos , nil ); //Leemos la data y la metemos en el string
  CloseHandle( Archivo );//Cerramos el handle
  Result := buffer; //rETORNMAOS EL STRIN CON LA DATA
end;

if Archivo = INVALID_HANDLE_VALUE then begin
  Result := 'ERROR'; //Si no podemos abrir el archivo tretornamos error1
end;

end;
Este coded es sencillo y muy util , perfecto para aprender!

Saludos!

Re: [Delphi]ArchivoToString - Lite By ØnLy

Publicado: 07 Dic 2010, 23:58
por The Swash
La verdad usas un IF demás.. por que no usar el else al terminar el IF que verifica si devuele o no un handle valido..
Sigue practicando.. pequeños detalles..
Gracias y saludos!

Re: [Delphi]ArchivoToString - Lite By ØnLy

Publicado: 08 Dic 2010, 00:02
por ØnLy
The Swash escribió:La verdad usas un IF demás.. por que no usar el else al terminar el IF que verifica si devuele o no un handle valido..
Sigue practicando.. pequeños detalles..
Gracias y saludos!
Muy Cierto , pero bueno esta funcion la hice hace tiempo.....!
Gracias de todas maneras por la correcion , las criticas constructuvas simepre son bienvenidas!

Re: [Delphi]ArchivoToString - Lite By ØnLy

Publicado: 04 Feb 2011, 01:44
por konanxp
Muy buen aporte y muy bien explicado, mi primer acercamiento a las APIs, se agradece, ahora a probarlo.