views:

199

answers:

3

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?

+3  A: 

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

dkackman
I agree. If the service is running as LocalSystem then it makes sense to store data in the user-independent data path.
Christian Hayter
+1  A: 

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);

Magnus Johansson
A: 

If this is a .NET service I think you could use IsolatedStorage

Locksfree