Where should a LocalSystem service store data files under Windows



I have a .Net 2.0 Windows service (C#) that runs as system and needs to store
state in files. The files are created and used only by the service. Where
should I store them?

I have tried using
but it is giving me inconsistent results. On initial install and start,
GetFolderPath returns the local data directory under the "Local Service"
profile. If I install SQL Server 2005 Express and then restart my service,
the API returns the local data directory under the "Network Service" profile.
If I reboot the system, my service comes up and gets the "Network Service"
profile again, but if I then stop and restart my service, it switches back to
the "Local Service" profile. What gives?

I have replicated this with a simple service generated with VS.Net 2005,
installed with InstallUtil and started with "net start". Here's the code for
my simple service:

const string filename = @"C:\test.txt";
protected override void OnStart(string[] args)
if( File.Exists(filename) )
using( StreamWriter w = new StreamWriter(filename) )
w.WriteLine("Local Data Directory: " +


One more note: the "moving data directories" behavior I describe is under
Windows XPsp2.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question