When the application (WinForms) starts, Environment.CurrentDirectory contains the path to the application folder (i.e. the folder containing the .exe assembly). Using any of the File Dialogs, for example. OpenFileDialog , SaveFileDialog etc. will change the current directory (if another folder is selected).
When you start a Windows service, its containing folder is C: \ Windows \ System32, since it is a system folder, and it is the system (that is, the operating system) that actually starts your Windows service.
Note that specifying a relative path in most System.IO objects will revert to using the Environment.CurrentDirectory property.
As already mentioned, there are several ways to get the path to the service executable using Assembly.GetEntryAssembly() or Assembly.GetExecutingAssembly() , and then using the Location property or CodeBase property (remember that this file is a path, not an executable directory).
Another option is to use:
`System.IO.Directory.SetCurrentDirectory( System.AppDomain.CurrentDomain.BaseDirectory );`
Make a call in the Service OnStart method, applying it to the entire application.
Schmuli
source share