Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where on the filesystem should a Windows service persist its data?

I'm writing a Windows service which needs to persist some data across reboots/restarts of the service. Currently I'm writing the files in a directory returned by Application.UserAppDataPath, but that doesn't seem to be giving me a consistent answer. How should I determine the right place to write the data?

like image 722
Paul Crowley Avatar asked Sep 23 '09 11:09

Paul Crowley


People also ask

Where are windows Service files stored?

The Services file is typically located in %windir%\System32\drivers\etc\services. If the file is missing, check with your system administrator before starting the database server installation process.

What is the difference between worker service and Windows service?

Both are real services. Windows Services have existed for over 20 years. They start most often at system startup and run permanently. A Worker Service is also a real process, but is intended as a background service for a front-end application; it starts with the application and stops with the application.

Which are service startup types?

Automatic – services will start at boot time. Automatic (Delayed Start) – services will start just after boot time. Manual – Start a service manually when needed. Disabled – Stop a service from starting, even if needed.


2 Answers

It depends if your service is running with the system account or with a specific user account.

  • System account. Store the files in the CommonApplicationData folder:

    string pathForSystem = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

  • User account. Store the files in the ApplicationData folder:

    string pathForUser = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

like image 170
Magnus Johansson Avatar answered Oct 12 '22 23:10

Magnus Johansson


If you want it to be consistent (i.e. user agnostic) try Application.CommonAppDataPath.

like image 42
dkackman Avatar answered Oct 12 '22 23:10

dkackman