Reputation: 269
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.
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. I am passing the relative path: .\..\Projects\SendMail\Template.html
to FileExists()
, but it's returning that the file does not exist.
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?
Upvotes: 2
Views: 7868
Reputation: 9872
My experience has been that services start with a working folder of %SystemRoot%\System32
no matter where the actual executable is located.
The way that I have got around this limitation is to write a registry key during installation of the service (e.g. HKLM\SOFTWARE\MyCompany\MyApp\INSTALL_PATH
) that points to what I would like the working folder to be. Then when the service starts, it grabs the data from the registry and uses that value as the base when creating paths to files.
Upvotes: 3
Reputation: 37215
You assume that the current directory of the service is the directory the executable is stored in. Call GetCurrentDir to find out the current directory.
Upvotes: 3
Reputation: 7062
As lorenzog said, try specifying the full path.
You can also try to set the currentdir to your likings.
//sets currentdir to your application.exe dir
SetCurrentDir(ExtractFileDir(ParamStr(0)));
Upvotes: 8