Delphi INI 读字符串限制

Delphi INI readstring limitation

我将应用程序的设置存储在 INI 文件中。我读到二进制条目有 2kb 的限制,所以我将二进制编码为字符串并将该值存储为字符串 (writestring)。检查文件时,似乎所有字符串都按预期存储。

尝试读回时,似乎只读取了 2047 个字符,因此在将其解码回流时失败。

显然,字符串似乎也有 2kb 的限制,但我想知道是否是这样,或者我做错了什么。如果有这样的限制,我知道如何绕过它吗?

谢谢

编辑: 愚蠢的我,我去了 system.inifiles 它在代码中说

function TIniFile.ReadString(const Section, Ident, Default: string): string;
var
  Buffer: array[0..2047] of Char; <<<<<<<<<<<<<<<<
begin

  SetString(Result, Buffer, GetPrivateProfileString(MarshaledString(Section),
    MarshaledString(Ident), MarshaledString(Default), Buffer, Length(Buffer),
    MarshaledString(FFileName)));
end;

解决方法很简单。

扩展 TInifile 并插入您自己的 ReadString 版本。

TMyIniFile = class(TInifile)
      function ReadString(const Section, Ident, Default: string): string; override;
end;

function TMyIniFile.ReadString(const Section, Ident, Default: string): string;
var
  Buffer: array[0..largenumber] of Char;
begin                                
  SetString(Result, Buffer, GetPrivateProfileString(MarshaledString(Section),
    MarshaledString(Ident), MarshaledString(Default), Buffer, Length(Buffer),
    MarshaledString(FFileName)));
end;