Home All Groups Group Topic Archive Search About

Where should a LocalSystem service store data files under Windows

Author
2 Feb 2007 10:57 PM
cypher
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
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
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) )
    {
        File.Delete(filename);
    }
    using( StreamWriter w = new StreamWriter(filename) )
    {
        w.WriteLine("Local Data Directory: " +
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
    }
}

Author
2 Feb 2007 11:37 PM
cypher
One more note: the "moving data directories" behavior I describe is under
Windows XPsp2.



Show quote
"cypher" wrote:

> 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
> Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
> 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) )
>     {
>         File.Delete(filename);
>     }
>     using( StreamWriter w = new StreamWriter(filename) )
>     {
>         w.WriteLine("Local Data Directory: " +
> Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
>     }
> }
>
>
>

AddThis Social Bookmark Button