Reputation: 59
I am trying to load a bunch of lines from a text file located in same directory as the .dproj and Delphi can't find the file I'm trying to read from. the code is as follows:
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;
Upvotes: 0
Views: 846
Reputation: 8396
If you are using default project options then your application executable isn't compiled into the project directory but instead into the Win32\Debug
or Win32\Release
subfolder of your project folder.
So you should take into account the relative path to your file. In your case, the desired file is in second parent folder of the folder in which your executable resides.
I recommend you first get the path location of your executable file using ExtractFilePath(Application.ExeName)
.
Then you can make use of TDirectory.GetParent()
in order to move up the directory chain until you reach the desired directory.
Upvotes: 6