Best place to store configuration files and log files in Windows for my program?

I need to store log files and configuration files for my application. Where is the best place to store them?

Right now, I'm just using the current directory, which ends up putting them in the Program Files directory where my program lives.

The log files are likely to be available to the user somewhat regularly, so %APPDATA% seems a bit inaccessible.

Is a directory better under %USERPROFILE%\My Documents ? It should work for all versions of Windows starting in 2000.

+41
c # windows
Nov 06 '08 at 19:08
source share
9 answers

If you are not using ConfigurationManager to manage your application and user settings, you should be. The configuration toolkit in the .NET Framework is remarkably well designed, and the Visual Studio tools that interact with it, too.

The default behavior of the ConfigurationManager sets both invariant (applications) and modifiable (user) settings in the right places: application settings are included in the application folder, and user settings go to System.Environment.SpecialFolder.LocalApplicationData . It works correctly on all versions of Windows that support .NET.

As for the log files, System.Environment.SpecialFolder.LocalApplicationData is usually the place you want to put as it is guaranteed to be written by the user.

Of course, there are times when you would not want to - for example, if you want to write files to a network resource so that you can easily access them remotely. There is a fairly wide range of ways to implement it, but most of them begin by creating an application parameter containing the path to the shared folder. All of them are related to administration.

I have several complaints about ConfigurationManager and VS-tools: there should be better documentation of a high level than there is, and better documentation of the VS Settings class. The mechanism by which the app.config file goes to the application configuration file in the target assembly directory is not transparent (and is the source of one of the most frequently asked questions: "what happened to my connection string?"). And if there is a way to create settings that do not have default values, I did not find it.

+28
Nov 06 '08 at 20:03
source share

For application parameters - use System.Environment.SpecialFolder.ApplicationData - data of the roaming profile is stored here, so it allows your user to register and work from different computers in the domain.

For log files - System.Environment.SpecialFolder.LocalApplicationData p>

+8
Nov 06 '08 at 19:32
source share

Note. You can get the path to the LocalApplicationData folder in .NET using the following function:

 string strPath=System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData); 
+8
May 19 '10 at 12:45 a.m.
source share

To be honest,% appdata% is still the best place to host configuration files and log files, as it serves to use a data warehouse to store your application data. This should not be difficult to access, just write% appdata% in explorer and you will be redirected directly to your% appdata% directory.

+5
Nov 06 '08 at 19:41
source share

The accepted answer notes that the following is a good place for log files. System.Environment.SpecialFolder.LocalApplicationData This corresponds to the path to C:\Users\[User]\AppData\Roaming that you see is user-specific. As in the accepted answer, this is a guaranteed place convenient for the user and can be useful for certain situations.

However, in a web application environment, you can run your application under a network account, and you or your colleague may need to try and track exactly where these logs go to the application. I personally would like to use the System.Environment.SpecialFolder.CommonApplicationData null user System.Environment.SpecialFolder.CommonApplicationData , which is C:\ProgramData . Yes, you will need to specify access rights for any folders you create, but this is usually a one-time deal, and then all application logs can live in one happy place.

In addition, when browsing the Internet, there is a project to programmatically set write access to folders created in CommonApplicationData , Allow recording / changing access to CommonApplicationData data .

+5
Oct 18
source share

Do not store configuration files in the application folder, Microsoft has stated that this is NOT an ideal place. Windows is moving to write locks in C: \ Program Files \ and you will see in Vista any application that tries to write here will trigger a UAC warning.

Windows 7 will allow users to customize the UAC pop-ups that they use (expect some users to block most of them), and your application will fail / freeze if the user never approves this write attempt.

If you use the correct userprofile and appdata variables, then Win 2000, XP, Vista, and Win7 will display the data in the appropriate write-friendly folder without UAC pop-ups.

+4
Nov 06 '08 at 19:19
source share

You can use SHGetSpecialFolderPath:

 int MAX_PATH = 255; CString m_strMyPath; SHGetSpecialFolderPath(NULL, m_strMyPath.GetBuffer(MAX_PATH), CSIDL_COMMON_APPDATA, TRUE); 

This field indicates the "special folder path" that can be safely written to windows:

For XP: C:\Documents and Settings\All Users\Application Data

For Vista: C:\ProgramData

Check out the MSDN page here: http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx

+2
Nov 06 '08 at 19:50
source share

The best answer depends on the nature of the logs and configurations. If they are software, and you do not need to survive the removal of the application, I think that they are in order where they are. If the logs and configurations are user-specific or you need to survive the deletion, then they belong somewhere in% USERPROFILE% -% APPDATA%, which is the "correct" base directory for this type of thing.

+1
Nov 06 '08 at 19:15
source share

I use isolation storage for configuration. You can also use the Temp folder to store temporary information, such as a log.

0
Nov 06 '08 at 19:13
source share



All Articles