在我的程序中,在Windows上存储配置文件和日志文件的最佳位置?

时间:2023-01-12 23:44:43

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.

现在,我只是使用当前目录,最终将它们放在我的程序所在的Program Files目录中。

The log files will probably be accessed by the user somewhat regularly, so %APPDATA% seems a little hard to get to.

用户可能会定期访问日志文件,因此%APPDATA%似乎有点难以实现。

Is a directory under %USERPROFILE%\My Documents the best? It needs to work for all versions of Windows, from 2000 forward.

%USERPROFILE%\ My Documents下的目录最好吗?从2000年开始,它需要适用于所有版本的Windows。

9 个解决方案

#1


31  

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

如果您没有使用ConfigurationManager来管理您的应用程序和用户设置,那么您应该这样做。 .NET Framework中的配置工具包经过深思熟虑,并且与其互操作的Visual Studio工具也是如此。

The default behavior of ConfigurationManager puts both invariant (application) and modifiable (user) settings in the right places: the application settings go in the application folder, and the user settings go in System.Environment.SpecialFolder.LocalApplicationData. It works properly under all versions of Windows that support .NET.

ConfigurationManager的默认行为将不变(应用程序)和可修改(用户)设置放在正确的位置:应用程序设置放在应用程序文件夹中,用户设置放在System.Environment.SpecialFolder.LocalApplicationData中。它在支持.NET的所有Windows版本下都能正常运行。

As for log files, System.Environment.SpecialFolder.LocalApplicationData is generally the place that you want to put them, because it's guaranteed to be user-writeable.

对于日志文件,System.Environment.SpecialFolder.LocalApplicationData通常是您要放置它们的位置,因为它保证是用户可写的。

There are certainly cases where you wouldn't - for instance, if you want to write files to a network share so that you easily can access them remotely. There's a pretty wide range of ways to implement that, but most of them start with creating an application setting that contains the path to the shared folder. All of them involve administration.

在某些情况下,您肯定不会 - 例如,如果您想将文件写入网络共享,以便您可以轻松地远程访问它们。有很多种方法可以实现,但大多数方法都是从创建包含共享文件夹路径的应用程序设置开始的。所有这些都涉及管理。

I have a couple of complaints about ConfigurationManager and the VS tools: there needs to be better high-level documentation than there is, and better documentation of the VS-generated Settings class. The mechanism by which the app.config file turns into the application configuration file in the target build directory is opaque (and the source of one of the most frequently asked questions of all: "what happened to my connection string?"). And if there's a way of creating settings that don't have default values, I haven't found it.

我有一些关于ConfigurationManager和VS工具的抱怨:需要更好的高级文档,以及更好的VS生成的Settings类文档。 app.config文件转换为目标构建目录中的应用程序配置文件的机制是不透明的(并且是所有问题中最常见问题之一的来源:“我的连接字符串发生了什么?”)。如果有一种方法可以创建没有默认值的设置,我还没有找到它。

#2


9  

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

注意:您可以使用以下函数获取.NET中LocalApplicationData文件夹的路径:

string strPath=System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);

#3


8  

For application settings - use System.Environment.SpecialFolder.ApplicationData - this is where a roaming profile data is stored, so it allows your user to log and work from different machines in the domain.

对于应用程序设置 - 使用System.Environment.SpecialFolder.ApplicationData - 这是存储漫游配置文件数据的位置,因此它允许您的用户从域中的不同计算机进行登录和工作。

For log files - System.Environment.SpecialFolder.LocalApplicationData

对于日志文件 - System.Environment.SpecialFolder.LocalApplicationData

#4


6  

To be honest %appdata% is still the best place to place your config files and log files, as it serves the purpose of a placeholder to store your application data. It should not be that hard to access, just write %appdata% in explorer and you will be directed straight to your %appdata% directory.

说实话,%appdata%仍然是放置配置文件和日志文件的最佳位置,因为它用于占位符来存储应用程序数据。它应该不难访问,只需在资源管理器中编写%appdata%,您将直接进入%appdata%目录。

#5


5  

The accepted answer notes that for log files the following is a good spot. System.Environment.SpecialFolder.LocalApplicationData This equates to a path of C:\Users\[User]\AppData\Roaming which you can see is user specific. Like the accepted answer mentions this is a guaranteed user-writeable location and can be useful for certain situations

接受的答案指出,对于日志文件,以下是一个好地方。 System.Environment.SpecialFolder.LocalApplicationData这相当于C:\ Users \ [User] \ AppData \ Roaming的路径,您可以看到该路径是特定于用户的。与接受的答案一样,提到这是一个有保证的用户可写位置,对某些情况很有用

However in a web application environment you may be running your application under a network account and you or a coworker may need to try and track down where exactly those logs are going per application. I personally like to use the non user specific location enumeration of System.Environment.SpecialFolder.CommonApplicationData which equates to C:\ProgramData. Yes, you will need to specify access rights for any folders you create, but it's usually a one time deal and then all of your application logs can live in one happy location.

但是,在Web应用程序环境中,您可能正在网络帐户下运行应用程序,而您或同事可能需要尝试跟踪每个应用程序的日志记录。我个人喜欢使用System.Environment.SpecialFolder.CommonApplicationData的非用户特定位置枚举,它等同于C:\ ProgramData。是的,您需要为您创建的任何文件夹指定访问权限,但这通常是一次性交易,然后您的所有应用程序日志都可以位于一个快乐的位置。

Additionally, while looking around the Internet, there is a project out there to programatically set write access to folders you create within CommonApplicationData, Allow write/modify access to CommonApplicationData.

此外,在浏览Internet时,还有一个项目以编程方式设置对您在CommonApplicationData中创建的文件夹的写访问权限,允许对CommonApplicationData进行写入/修改访问。

#6


4  

Do not store config files in the application folder, Microsoft has stated this is NOT the ideal location. Windows has been moving towards blocking writing to C:\Program Files\ and you'll find in Vista any application that tries to write here, will fire up a UAC warning.

不要将配置文件存储在应用程序文件夹中,Microsoft已声明这不是理想的位置。 Windows一直在阻止写入C:\ Program Files \,你会发现在Vista中任何试图写在这里的应用程序都会启动UAC警告。

Windows 7 will allow users to customize what UAC popups they use (expect some power users to block most of them) and your app will fail/freeze if the user never approves this write attempt.

Windows 7将允许用户自定义他们使用的UAC弹出窗口(期望一些高级用户阻止他们中的大多数),如果用户从未批准此写入尝试,您的应用程序将失败/冻结。

If you use the proper userprofile and appdata variables, then Win 2000, XP, Vista, and Win7 will map the data to the proper write friendly folder, with no UAC popups.

如果你使用正确的userprofile和appdata变量,那么Win 2000,XP,Vista和Win7会将数据映射到正确的写友好文件夹,没有UAC弹出窗口。

#7


2  

You can use SHGetSpecialFolderPath:

您可以使用SHGetSpecialFolderPath:

int MAX_PATH = 255;

CString m_strMyPath;

SHGetSpecialFolderPath(NULL, m_strMyPath.GetBuffer(MAX_PATH), CSIDL_COMMON_APPDATA, TRUE);

This will specify the 'special folder path' which you can safely write logs to for windows:

这将指定“特殊文件夹路径”,您可以安全地为Windows写入日志:

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

对于XP:C:\ Documents and Settings \ All Users \ Application Data

For Vista: C:\ProgramData

对于Vista:C:\ ProgramData

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

在这里查看MSDN页面:http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx

#8


1  

The best answer depends on the nature of the logs and configurations. If they are program-wide, and don't need to survive uninstallation of the application, then I think they're fine where they are. If the logs and configurations are user specific, or need to survive uninstallation, then they belong somewhere under %USERPROFILE% - %APPDATA% being the 'proper' base directory for this type of thing.

最佳答案取决于日志和配置的性质。如果它们是程序范围的,并且不需要在应用程序的卸载后继续存在,那么我认为它们很好。如果日志和配置是特定于用户的,或者需要在卸载后继续存在,那么它们属于%USERPROFILE% - %APPDATA%,这是此类事物的“正确”基本目录。

#9


0  

I use the Isolation Storage for configuration. You can also use the Temp folder to store temporary information like log.

我使用隔离存储进行配置。您还可以使用Temp文件夹存储临时信息,如日志。

#1


31  

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

如果您没有使用ConfigurationManager来管理您的应用程序和用户设置,那么您应该这样做。 .NET Framework中的配置工具包经过深思熟虑,并且与其互操作的Visual Studio工具也是如此。

The default behavior of ConfigurationManager puts both invariant (application) and modifiable (user) settings in the right places: the application settings go in the application folder, and the user settings go in System.Environment.SpecialFolder.LocalApplicationData. It works properly under all versions of Windows that support .NET.

ConfigurationManager的默认行为将不变(应用程序)和可修改(用户)设置放在正确的位置:应用程序设置放在应用程序文件夹中,用户设置放在System.Environment.SpecialFolder.LocalApplicationData中。它在支持.NET的所有Windows版本下都能正常运行。

As for log files, System.Environment.SpecialFolder.LocalApplicationData is generally the place that you want to put them, because it's guaranteed to be user-writeable.

对于日志文件,System.Environment.SpecialFolder.LocalApplicationData通常是您要放置它们的位置,因为它保证是用户可写的。

There are certainly cases where you wouldn't - for instance, if you want to write files to a network share so that you easily can access them remotely. There's a pretty wide range of ways to implement that, but most of them start with creating an application setting that contains the path to the shared folder. All of them involve administration.

在某些情况下,您肯定不会 - 例如,如果您想将文件写入网络共享,以便您可以轻松地远程访问它们。有很多种方法可以实现,但大多数方法都是从创建包含共享文件夹路径的应用程序设置开始的。所有这些都涉及管理。

I have a couple of complaints about ConfigurationManager and the VS tools: there needs to be better high-level documentation than there is, and better documentation of the VS-generated Settings class. The mechanism by which the app.config file turns into the application configuration file in the target build directory is opaque (and the source of one of the most frequently asked questions of all: "what happened to my connection string?"). And if there's a way of creating settings that don't have default values, I haven't found it.

我有一些关于ConfigurationManager和VS工具的抱怨:需要更好的高级文档,以及更好的VS生成的Settings类文档。 app.config文件转换为目标构建目录中的应用程序配置文件的机制是不透明的(并且是所有问题中最常见问题之一的来源:“我的连接字符串发生了什么?”)。如果有一种方法可以创建没有默认值的设置,我还没有找到它。

#2


9  

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

注意:您可以使用以下函数获取.NET中LocalApplicationData文件夹的路径:

string strPath=System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);

#3


8  

For application settings - use System.Environment.SpecialFolder.ApplicationData - this is where a roaming profile data is stored, so it allows your user to log and work from different machines in the domain.

对于应用程序设置 - 使用System.Environment.SpecialFolder.ApplicationData - 这是存储漫游配置文件数据的位置,因此它允许您的用户从域中的不同计算机进行登录和工作。

For log files - System.Environment.SpecialFolder.LocalApplicationData

对于日志文件 - System.Environment.SpecialFolder.LocalApplicationData

#4


6  

To be honest %appdata% is still the best place to place your config files and log files, as it serves the purpose of a placeholder to store your application data. It should not be that hard to access, just write %appdata% in explorer and you will be directed straight to your %appdata% directory.

说实话,%appdata%仍然是放置配置文件和日志文件的最佳位置,因为它用于占位符来存储应用程序数据。它应该不难访问,只需在资源管理器中编写%appdata%,您将直接进入%appdata%目录。

#5


5  

The accepted answer notes that for log files the following is a good spot. System.Environment.SpecialFolder.LocalApplicationData This equates to a path of C:\Users\[User]\AppData\Roaming which you can see is user specific. Like the accepted answer mentions this is a guaranteed user-writeable location and can be useful for certain situations

接受的答案指出,对于日志文件,以下是一个好地方。 System.Environment.SpecialFolder.LocalApplicationData这相当于C:\ Users \ [User] \ AppData \ Roaming的路径,您可以看到该路径是特定于用户的。与接受的答案一样,提到这是一个有保证的用户可写位置,对某些情况很有用

However in a web application environment you may be running your application under a network account and you or a coworker may need to try and track down where exactly those logs are going per application. I personally like to use the non user specific location enumeration of System.Environment.SpecialFolder.CommonApplicationData which equates to C:\ProgramData. Yes, you will need to specify access rights for any folders you create, but it's usually a one time deal and then all of your application logs can live in one happy location.

但是,在Web应用程序环境中,您可能正在网络帐户下运行应用程序,而您或同事可能需要尝试跟踪每个应用程序的日志记录。我个人喜欢使用System.Environment.SpecialFolder.CommonApplicationData的非用户特定位置枚举,它等同于C:\ ProgramData。是的,您需要为您创建的任何文件夹指定访问权限,但这通常是一次性交易,然后您的所有应用程序日志都可以位于一个快乐的位置。

Additionally, while looking around the Internet, there is a project out there to programatically set write access to folders you create within CommonApplicationData, Allow write/modify access to CommonApplicationData.

此外,在浏览Internet时,还有一个项目以编程方式设置对您在CommonApplicationData中创建的文件夹的写访问权限,允许对CommonApplicationData进行写入/修改访问。

#6


4  

Do not store config files in the application folder, Microsoft has stated this is NOT the ideal location. Windows has been moving towards blocking writing to C:\Program Files\ and you'll find in Vista any application that tries to write here, will fire up a UAC warning.

不要将配置文件存储在应用程序文件夹中,Microsoft已声明这不是理想的位置。 Windows一直在阻止写入C:\ Program Files \,你会发现在Vista中任何试图写在这里的应用程序都会启动UAC警告。

Windows 7 will allow users to customize what UAC popups they use (expect some power users to block most of them) and your app will fail/freeze if the user never approves this write attempt.

Windows 7将允许用户自定义他们使用的UAC弹出窗口(期望一些高级用户阻止他们中的大多数),如果用户从未批准此写入尝试,您的应用程序将失败/冻结。

If you use the proper userprofile and appdata variables, then Win 2000, XP, Vista, and Win7 will map the data to the proper write friendly folder, with no UAC popups.

如果你使用正确的userprofile和appdata变量,那么Win 2000,XP,Vista和Win7会将数据映射到正确的写友好文件夹,没有UAC弹出窗口。

#7


2  

You can use SHGetSpecialFolderPath:

您可以使用SHGetSpecialFolderPath:

int MAX_PATH = 255;

CString m_strMyPath;

SHGetSpecialFolderPath(NULL, m_strMyPath.GetBuffer(MAX_PATH), CSIDL_COMMON_APPDATA, TRUE);

This will specify the 'special folder path' which you can safely write logs to for windows:

这将指定“特殊文件夹路径”,您可以安全地为Windows写入日志:

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

对于XP:C:\ Documents and Settings \ All Users \ Application Data

For Vista: C:\ProgramData

对于Vista:C:\ ProgramData

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

在这里查看MSDN页面:http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx

#8


1  

The best answer depends on the nature of the logs and configurations. If they are program-wide, and don't need to survive uninstallation of the application, then I think they're fine where they are. If the logs and configurations are user specific, or need to survive uninstallation, then they belong somewhere under %USERPROFILE% - %APPDATA% being the 'proper' base directory for this type of thing.

最佳答案取决于日志和配置的性质。如果它们是程序范围的,并且不需要在应用程序的卸载后继续存在,那么我认为它们很好。如果日志和配置是特定于用户的,或者需要在卸载后继续存在,那么它们属于%USERPROFILE% - %APPDATA%,这是此类事物的“正确”基本目录。

#9


0  

I use the Isolation Storage for configuration. You can also use the Temp folder to store temporary information like log.

我使用隔离存储进行配置。您还可以使用Temp文件夹存储临时信息,如日志。