[英]Where should my application/service store its configuration files in windows?
In Linux, I'll usually put user configuration files in $HOME/.myapp and in Windows I'll use APPDATA.在 Linux 中,我通常将用户配置文件放在 $HOME/.myapp 中,而在 Windows 中,我将使用 APPDATA。 So far so good.到现在为止还挺好。
What about non-user specific configuration?非用户特定的配置呢? In linux, I'd put it into /etc
.在 linux 中,我会将它放入/etc
。 Is there an equivalent in Windows? Windows 中是否有等价物? Please note I would like to have the service running before any user logs in. Also, in case it plays a role, I'm developing in Java.请注意,我希望在任何用户登录之前运行该服务。此外,如果它起作用,我正在用 Java 进行开发。
Alternatively: I approaching this the wrong way?或者:我以错误的方式接近这个?
You could use ALLUSERPROFILES as a base directory.您可以使用 ALLUSERPROFILES 作为基本目录。 This environment variable resolves to the C:\\PROGRAMDATA folder in Windows7.此环境变量解析为 Windows7 中的 C:\\PROGRAMDATA 文件夹。 Of course you need to add a specific folder for your applications当然你需要为你的应用程序添加一个特定的文件夹
In summary, you should use the known folder: ProgramData .总之,您应该使用已知文件夹: ProgramData 。
To avoid hard coding of paths (and hence why I'm not providing them here) you should always retrieve the value via one of the following methods:为了避免对路径进行硬编码(因此我没有在此处提供它们),您应该始终通过以下方法之一检索值:
%PROGRAMDATA%
environment variable (you can also use %ALLUSERSPROFILE%
but I consider %PROGRAMDATA%
more meaningful) %PROGRAMDATA%
环境变量(您也可以使用%ALLUSERSPROFILE%
但我认为%PROGRAMDATA%
更有意义)SHGetKnownFolderPath
, passing the FOLDERID_ProgramData
.对于非托管代码,使用SHGetKnownFolderPath
,传递FOLDERID_ProgramData
。System.Environment.GetFolderPath
, passing CommonApplicationData
.对于托管代码使用System.Environment.GetFolderPath
,传递CommonApplicationData
。This folder is not writeable by non-admins, so depending on your requirements you'll want to create a directory for your program and set the ACLs you need at install time.该文件夹不能由非管理员写入,因此根据您的要求,您需要为您的程序创建一个目录并在安装时设置您需要的 ACL。
Some good information on this topic is provided in the blog post: " Where Should I Write Program Data Instead of Program Files ".博客文章中提供了有关此主题的一些很好的信息:“ 我应该在哪里编写程序数据而不是程序文件”。
For those interested in using other Known Folders , MSDN provides extensive documentation .对于那些对使用其他已知文件夹感兴趣的人,MSDN 提供了大量文档。
In Windows most "program files" for an app go in C:\\Program Files\\MyApp.在 Windows 中,应用程序的大多数“程序文件”位于 C:\\Program Files\\MyApp。 The environment variable would be %ProgramFiles%\\MyApp.环境变量将是 %ProgramFiles%\\MyApp。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.