Mega Code Archive

 
Categories / Delphi / Files
 

Bir dosyanın text dosyası mı yoksa binary dosya mı olduğunu bulma

function IsTextFile(const sFile: TFileName): boolean; var oIn: TFileStream; iRead: Integer; iMaxRead: Integer; iData: Byte; dummy:string; begin result:=true; dummy :=''; oIn := TFileStream.Create(sFile, fmOpenRead or fmShareDenyNone); try iMaxRead := 1000; //only text the first 1000 bytes if iMaxRead > oIn.Size then iMaxRead := oIn.Size; for iRead := 1 to iMaxRead do begin oIn.Read(iData, 1); if (idata) > 127 then result:=false; end; finally FreeAndNil(oIn); end; end; (* ----- Sample call ----- *) procedure TForm1.Button1Click(Sender: TObject); begin if OpenDialog1.Execute then begin if IsTextFile(OpenDialog1.FileName) then showmessage('is ascii') else showmessage('is BinaryFile') end; end;