就象UltraEdit一样,打开一个文件,如果是可以显示的内容,比如文本就显示它,否则,就用十六进制表现。比如读Delphi生成的一些文件:
*.pas;*.cfg;*.dfm;*.dpr;*.dof 都是可以显示其内容的;
而*.dcu;*.res;*.exe 则是二进制文件,不能直接读懂。我要怎么判断呢?(要考虑中文)

解决方案 »

  1.   

    这个函数是以前在网上找的function IsTextFile(FileName:string):boolean;
    var
      Fs:TFileStream;
      i,size:integer;
      IsTextFile:boolean;
      ByteData:Byte;
    begin
      if FileExists(FileName) then
      begin
        Fs:=TFileStream.Create(FileName,fmOpenRead);
        IsTextFile:=true;
        i:=0;
        size:=Fs.Size;
        While (i<size) and IsTextFile do
        begin
          Fs.Read(ByteData,1);
          IsTextFile:=ByteData<>0;
          inc(i)
        end;
        Fs.Free;
        Result:=IsTextFile
      end
      else
        Result:=false
    end;