如何在Lazarus中使用相对文件路径

huangapple go评论113阅读模式
英文:

How to use relative file paths in Lazarus

问题

I'm creating a simple CRM in Lazarus with an SQLite backend but I can't figure out how to give Lazarus the file path to the database file relatively. This is needed for when I publish the app as I won't know what the full file path is on different target computers.

In Delphi, I know you can just put the file name of the database (with no further file path) in the connection string property of the connector and it'll work as long as that file is in the project folder but the equivalent in Lazarus (DatabaseName) doesn't allow relative file paths to be put there.

There are supposedly solutions on the Lazarus Forum, but they look complicated and I'm not understanding them.

Is it not possible to do this in a simple way on Lazarus, in which case am I better to try to run Python from Lazarus to get an absolute path from a relative path?

I need the relative paths to work on both Windows and MacOS.

英文:

I'm creating a simple CRM in Lazarus with an SQLite backend but I can't figure out how to give Lazarus the file path to the database file relativly. This is needed for when I publish the app as I won't know what the full file path is on different target computers.

In Delphi, I know you can just put the file name of the database (with no further file path) in the connection string property of the connector and it'll work as long as that file is in the project folder but the equivalent in Lazarus (DatabaseName) doesn't allow relative file paths to be put there.

There are supposedly solutions on the Lazarus Forum, but they look complicated and I'm not understanding them.

Is it not possible to do this in a simple way on Lazarus, in which case am I better to try to run Python from Lazarus to get an absolute path from a relative path?

I need the relative paths to work on both Windows and MacOS

答案1

得分: 3

对我来说,在Delphi中也从未在所有情况下普遍有效,尽管有时取决于你如何打开项目,它可能在IDE中工作。这也可能取决于你使用什么作为数据库组件。

无论如何,在Windows上,你可以使用以下方法找到工作目录:

sExePath := ExtractFilePath(paramstr(0));

对于控制台程序,对于GUI应用程序,可以使用以下方法:

sExePath := ExtractFilePath(Application.ExeName);

然后,你可以使用以下方式构建路径:

includetrailingpathdelimiters(sExepath)+relativepath

并在启动时设置属性。

后来补充:

请注意,过去我曾允许我的应用程序在启动时更改工作目录。我放弃了这种方法,因为文件->打开菜单可能会更改工作目录,从而使结果变得脆弱。因此,最终我重新编写了所有的路径构建,以相对于我在启动时建立的目录。

英文:

For me that never worked universally in Delphi either, though sometimes depending on how you open the project it might work in the IDE . It might also depend on what you use as database components.

Anyway, you can find the working dir on Windows using

sExePath := ExtractFilePath(paramstr(0));

for console programs, while for for GUI apps you can do

sExePath := ExtractFilePath(Application.ExeName);

You can then craft the path using

includetrailingpathdelimiters(sExepath)+relativepath

and set the property on startup.

added later:

Note that in the past I let my applications change the working directory to this path on startup. I abandoned that approach since file->open menus could change working directory, making the result fragile So in the end I recoded all path crafting to be relative to a directory that I establish at start-up

huangapple
  • 本文由 发表于 2023年7月28日 00:47:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定