频道分类

Delphi memo 手动选择txt文本编码并读取

作者:admin 来源: 日期:2020/9/27 21:14:10 人气: 标签:

 
(前略)

  private
    { Private  }
    FFileName: String;    
    FEncodingIndex: Integer;
  public
    { Public  }
  end;
(后略)



var
  Enc: TEncoding;
begin
  if OpenTextFileDialog1.Execute then
    begin

      FFileName := OpenTextFileDialog1.FileName;
      FEncodingIndex := OpenTextFileDialog1.EncodingIndex;
      // EncodingIndex delphitop.com
      case FEncodingIndex of  
        1: Enc := TEncoding.Unicode;
        2: Enc := TEncoding.BigEndianUnicode;
        3: Enc := TEncoding.UTF8;
      else
        Enc := TEncoding.Default; // 0: ANSI
      end;

      Memo1.Lines.LoadFromFile(FFileName, Enc); 
    end;



procedure TForm1.FormCreate(Sender: TObject);
begin
  FFileName := '';
  FEncodingIndex := 0;
end;