Reputation: 3562
If I have an application that needs to store some data at runtime to the filesystem (say, something like a SQLite database or cache files), what is the correct folder to store it in?
On *nix it would be under /var
, which is straightforward, but on Windows I can't seem to find a proper recommendation.
Obviously it's not Program Files
because applications don't have write-access to there at runtime. It's also obviously not in any system folder such as Windows
.
If it's an application that a user runs then %APPDATA%
seems like the correct place, but what if the application is something like a service that runs as Network Service
(e.g. an ASP.Net application running under IIS that generates files)?
Upvotes: 2
Views: 1280
Reputation: 36308
The built-in service accounts do actually have Application Data folders which you could use:
C:\Windows\ServiceProfiles\LocalService\AppData
C:\Windows\ServiceProfiles\NetworkService\AppData
C:\Windows\System32\config\systemprofile\AppData
However, that would mean that the data won't be available if you change which account the service runs in, and also makes it harder for the system administrator to find.
A better choice is the Program Data folder, which in modern versions of Windows is usually found at
C:\ProgramData
In .NET, you can retrieve the path to the Program Data folder using Environment.GetFolderPath
, i.e.,
System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
You should create a folder inside the Program Data folder with an appropriate name, e.g., the name of your application and/or company. Depending on context, you may also need to change the permissions on your subfolder.
Upvotes: 1
Reputation: 1254
In c# you can use
System.IO.Path.GetTempPath()
which gives you the path:
C:\Documents and Settings\User\Local Settings\Temp\
For not temproray file you can use:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Hope that's what you looking for
Upvotes: 0