views:

583

answers:

3

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?

+3  A: 

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)));
The_Fox
+1  A: 

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.

devio
Ok, I just found the directory: C:\Windows\system32\. Thank you very much.
DelphiProgrammer
A: 

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.

Scott W
Gooooooooooooooood, very good!!! Thanks my friend. That's it... now it works.
DelphiProgrammer