哪里可以安全地存储与 XP、Vista 和 Windows 7 兼容的日志文件?
我有一个 winforms 应用程序需要写出日志。该应用程序还需要在没有 UAC 警告的情况下运行并使用非管理员帐户。
我可以在哪里写入日志而不与默认安全设置发生冲突?此外,这必须适用于 Windows XP、Vista 和 7。
如果所有用户都可以写入同一组日志文件,那就加分了。
I have a winforms app that needs to write out logs. The app also needs to run without UAC warnings and using a non-admin account.
Where can I write my logs without running afoul of the default security settings? In addition, this has to work on Windows XP, Vista, and 7.
Extra points if all users can write to the same set of log files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能(不应该)对路径进行硬编码。相反,通过调用 SHGetFolderPath 询问 Windows 将其存储在哪里,该路径可用于返回当前用户(或所有用户)AppData 目录的位置。
MSDN文章,带有C解决方案:
http://support.microsoft.com/kb/310294
有用的 Delphi 示例:
http://delphi.about.com/od/kbwinshell/a/SHGetFolderPath。 htm
请注意,对路径进行一次性查询、转换为绝对字符串并将其存储在您的设置中是危险的。因为 Windows 可以改变它。他们可以加入域、升级 Windows 等,并且 Windows 确实可能会移动您的文件。如果您随后指向旧位置,而不再次询问“此文件夹在哪里”,您就会崩溃。去过那里,做过那件事...
You cannot (should not) hard-code the path. Instead, ask Windows where to store it by calling SHGetFolderPath, which can be used to return the location of the current user's (or all users) AppData directory.
MSDN article, with C solution:
http://support.microsoft.com/kb/310294
Useful Delphi example:
http://delphi.about.com/od/kbwinshell/a/SHGetFolderPath.htm
Note that it is hazardous to do a one-time query for the path, convert to an absolute string, and store that in your settings. Because Windows can change it. They can join a domain, upgrade windows, etc., and windows may indeed move your files. If you then point at the old location, without asking again "where is this folder", you'll break. Been there, done that...
使用 SHGetFolderPath 您可以选择使用每个用户或全局日志文件目录。如果您使用 CSIDL_COMMON_APPDATA 则: “这些信息不会漫游,任何使用该计算机的人都可以获得。”听起来这就是你想要的。
Using SHGetFolderPath you have the option of using per-user or global log file directories. If you use CSIDL_COMMON_APPDATA then: "This information will not roam and is available to anyone using the computer." which sounds like it's what you want.