The system cannot find the file specified

微笑、不失礼 提交于 2020-01-15 04:56:07

问题


The file is inside the directory where the software is. I am trying to add the text file to the memo box.

procedure TForm4.FormCreate(Sender: TObject);
var
  dir : string;
begin
  Form4.Caption:='Abateri instrumente';
  dir := GetCurrentDir;
  Memo1.Lines.LoadFromFile(dir+'\abateri.txt');
end;

回答1:


In your specific situation, you should load the file with the code

Memo1.Lines.LoadFromFile(dir+'\abateri.txt.txt');

This is because in the below screenshot that you provided, the extension of the Project3 file is hidden, which loads to the conclusion that the option to hide known file extensions is enabled. Yet the one for the abateri.txt file is shown, which can only lead to the often seen double extension mistake.

Either rename your file and remove the redundant part (the first .txt, which is preferred) or use the double extension in your code.
I would also suggest disabling that option in Windows Explorer:
Tools > Folder Options > View > Uncheck the "Hide extensions of known file types"

In addition to the above, you should always build up paths with the TPath.Combine function call to ensure that they are correct. You can see the documentation of it here




回答2:


The file is inside the directory where the software is.

In that case, looking in the working directory is the wrong approach. There's no reason why the working directory should be directory where your executable resides. You need to use:

Dir := ExtractFilePath(ParamStr(0)); // the directory where the executable resides
TPath.Combine(Dir, FileName); // TPath is from the System.IOUtils unit

Of course, your other problem is that you got your file name wrong. The file is actually named abateri.txt.txt.



来源:https://stackoverflow.com/questions/29875727/the-system-cannot-find-the-file-specified

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!