C# 在 Windows 上为我的程序存储配置文件和日志文件的最佳位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/269893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best place to store configuration files and log files on Windows for my program?
提问by Dave
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。
采纳答案by Robert Rossney
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
文件变成目标构建目录中的应用程序配置文件的机制是不透明的(也是最常见问题之一的来源:“我的连接字符串发生了什么?”)。如果有一种创建没有默认值的设置的方法,我还没有找到。
回答by Patrick Desjardins
I use the Isolation Storagefor configuration. You can also use the Tempfolder to store temporary information like log.
我使用隔离存储进行配置。您还可以使用Temp文件夹来存储日志等临时信息。
回答by Harper Shelby
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% 是此类事物的“正确”基本目录。
回答by TravisO
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.
不要将配置文件存储在应用程序文件夹中,微软表示这不是理想的位置。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 弹出窗口。
回答by Sunny Milenov
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
回答by RWendi
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% 目录。
回答by Klathzazt
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
回答by WWC
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);
回答by Dylan Hayes
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 的写/修改访问权限。