在 Delphi (FMX) 中找不到文件

Ash*_*Ash 0 delphi firemonkey

我正在尝试从与 .dproj 位于同一目录的文本文件中加载一堆行,但 Delphi 找不到我试图从中读取的文件。代码如下:

    procedure TFoPrincipale.Button2Click(Sender: TObject);
var monFich : TextFile;
    Sligne : string;
begin
try
AssignFile(monFich, 'docText.txt');
Reset(monFich); 
except
      showmessage('Le fichier est introuvable');
      exit;
end;

  while not Eof (monFich) do
  begin
    Readln(monFich, Sligne);
    Memo1.Lines.Add(Sligne);
  end;

CloseFile(monFich);

end;
Run Code Online (Sandbox Code Playgroud)

文本文件

Sil*_*ior 6

如果您使用默认项目选项,那么您的应用程序可执行文件不会编译到项目目录中,而是编译到项目文件夹的Win32\DebugWin32\Release子文件夹中。

所以你应该考虑到你的文件的相对路径。在您的情况下,所需文件位于可执行文件所在文件夹的第二个父文件夹中。

我建议您首先使用ExtractFilePath(Application.ExeName).

然后,您可以利用TDirectory.GetParent()以向上移动目录链,直到到达所需的目录。