[英]Relative path of a given file from a service application in Delphi
I have a problem loading a file, as I'm passing a relative path to the function FileExists(Filename: String)
and it's returning false, that is, it does not find the file in the directory that I pass. 我在加载文件时遇到问题,因为我正在传递函数
FileExists(Filename: String)
的相对路径,并且它返回false,也就是说,它在我传递的目录中找不到该文件。
I have a file named Template.html
in the D:\\Programming\\Delphi\\Projects\\SendMail
directory, and a service written in Delphi whose .EXE
is in the D:\\Programming\\Delphi\\Automation
directory. 我在
D:\\Programming\\Delphi\\Projects\\SendMail
目录中有一个名为Template.html
的文件,以及一个用Delphi编写的服务,其.EXE
位于D:\\Programming\\Delphi\\Automation
目录中。 I am passing the relative path: .\\..\\Projects\\SendMail\\Template.html
to FileExists()
, but it's returning that the file does not exist. 我传递相对路径:
.\\..\\Projects\\SendMail\\Template.html
到FileExists()
,但它返回该文件不存在。
I think that has something to do with the relative path of a service and the relative path of the application being different. 我认为这与服务的相对路径和应用程序的相对路径不同有关。 Can anybody help me with this?
任何人都可以帮我吗?
As lorenzog said, try specifying the full path. 正如lorenzog所说,尝试指定完整路径。
You can also try to set the currentdir to your likings. 您也可以尝试将currentdir设置为您的喜好。
//sets currentdir to your application.exe dir
SetCurrentDir(ExtractFileDir(ParamStr(0)));
您假定服务的当前目录是存储可执行文件的目录。调用GetCurrentDir以查找当前目录。
My experience has been that services start with a working folder of %SystemRoot%\\System32
no matter where the actual executable is located. 我的经验是,无论实际可执行文件位于何处,服务都以
%SystemRoot%\\System32
的工作文件夹开头。
The way that I have got around this limitation is to write a registry key during installation of the service (eg HKLM\\SOFTWARE\\MyCompany\\MyApp\\INSTALL_PATH
) that points to what I would like the working folder to be. 我已解决此限制了该方法是安装的服务(例如在写一个注册表项
HKLM\\SOFTWARE\\MyCompany\\MyApp\\INSTALL_PATH
指向什么,我想将工作文件夹来定)。 Then when the service starts, it grabs the data from the registry and uses that value as the base when creating paths to files. 然后,当服务启动时,它会从注册表中获取数据,并在创建文件路径时使用该值作为基础。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.