我应该在哪里存储应用程序特定的设置?

发布于 2024-10-03 22:58:24 字数 753 浏览 0 评论 0原文

我被要求更新过去 6 年在 WinXP 上运行的 VB6 应用程序。客户想要使用 Windows 7。到目前为止,应用程序将其设置存储在位于应用程序目录中的 INI 文件中。 XP 和 7 之间的一个主要区别是您无法再写入 C:\Program Files\AppFolder

我想弄清楚应该在文件系统的哪个位置存储设置?鉴于该应用程序仍然需要在 WinXP 上运行,我有点困惑。

在 WinXP 上,我有以下内容:

C:\Documents and Settings\profilename\Application Data
C:\Documents and Settings\profilename\Local Settings\Application Data

在 Windows 7 上,我有以下内容:

C:\Users\profilename\AppData\Local
C:\Users\profilename\AppData\LocalLow
C:\Users\profilename\AppData\Roaming

这些文件夹中的每一个都有似乎存储各种产品的设置/文件的子文件夹

所以 2 个问题:

  1. 给定所有这些文件夹,我在哪里存储我的设置?
  2. 我假设有一个漂亮的 Windows API 调用可以为我提供该文件夹的正确位置。我希望它能在 XP 和 7 上运行。我的假设正确吗?如果是这样,链接将不胜感激。

I've been asked to update a VB6 application that's been running on WinXP for the last 6 years. The client wants to use Windows 7. Up until now, the app stored its settings in an INI file located in the application directory. One key difference between XP and 7 is that you can't write to C:\Program Files\AppFolder anymore.

I am trying to figure out where on the file system should I store settings? Given that the application is still required to run on WinXP, I am kind of confused.

On WinXP, I have the following:

C:\Documents and Settings\profilename\Application Data
C:\Documents and Settings\profilename\Local Settings\Application Data

On Windows 7, I have the following:

C:\Users\profilename\AppData\Local
C:\Users\profilename\AppData\LocalLow
C:\Users\profilename\AppData\Roaming

Each one of these folders have subfolders that seem to store settings/files for various products

So 2 questions:

  1. Given all these folders, where do I store my settings?
  2. I am assuming that there is a nifty Windows API call that would give me the proper location of this folder. And I am hoping it works on both XP and 7. Is my assumption correct? If so, a link would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪冰清 2024-10-10 22:58:24

在 XP/Vista/Windows 7 上,您可以使用许多特殊文件夹:

  • CSIDL_APPDATA 文件夹是您可能最感兴趣的文件夹。漫游用户可以在任何时间访问此处存储的数据。他们登录的机器。这是存储简单配置数据的最佳位置。所有用户都具有对此(以及最后一个)文件夹的写入权限。请注意,上述文件夹均不适用于用户生成的数据!这应该属于“我的文档”层次结构。
  • 编辑:正如 Cody Gray 在评论中建议的那样,还应考虑将 CSIDL_LOCAL_APPDATA 用于始终位于当前计算机本地但按每个用户预留的应用程序数据。此文件夹中的数据不可在漫游时使用,因此用户登录到其他计算机时可能不会错过这些数据。

我无耻地从Karl Peterson的好文章中复制了上面的解释,为VB6解释了这一点程序员。 Karl 还有一个 即用类 可以帮助您找到目录,但恕我直言这次他把事情搞得太复杂了。 Bob Riemersma 有一种更好的方法,使用 Shell 对象,如下。 编辑鲍勃下面的评论解释了为什么最好使用后期绑定而不是早期绑定

Const ssfCOMMONAPPDATA = &H23 
Const ssfLOCALAPPDATA = &H1c
Const ssfAPPDATA = &H1a
Dim strAppData As String 
 
strAppData = _ 
    CreateObject("Shell.Application").NameSpace(ssfAPPDATA).Self.Path 

在我看来,继续使用这些目录中的 INI 文件

There are a number of special folders you can use, on XP/Vista/Windows 7:

  • The CSIDL_APPDATA folder is the one you will likely be most interested in. Data stored here is available to roaming users at whatever machine they log in to. This is the best place to store simple configuration data. All users have write access to this (and the last) folder. Note that none of the above folders are for user-generated data! That would properly belong under the My Documents hierarchy.
  • EDIT: As Cody Gray suggests in the comments, also consider CSIDL_LOCAL_APPDATA for application data that will always be local to the current machine, but is set aside on a per user basis. The data in this folder is not available on a roaming basis, so it should be data that the user will likely not miss if they log in to a different machine.

I shamelessly copied the explanation above from a good article by Karl Peterson, explaining this for VB6 programmers. Karl also has a ready-to-use class that will help you find the directories, but IMHO he's overcomplicated things this time. Bob Riemersma has a better way in one line, using the Shell object, as below. EDIT Bob's comment below explains why it's best to use late binding for this rather than early binding.

Const ssfCOMMONAPPDATA = &H23 
Const ssfLOCALAPPDATA = &H1c
Const ssfAPPDATA = &H1a
Dim strAppData As String 
 
strAppData = _ 
    CreateObject("Shell.Application").NameSpace(ssfAPPDATA).Self.Path 

In my opinion it's fine to continue to use INI files in these directories.

静谧幽蓝 2024-10-10 22:58:24

也许您只是将设置保存在 Windows 注册表中?
这很容易。使用 SaveSeting 和 GetSetting 比创建 INI 文件容易得多。
而且兼容性上没有任何问题,从WinNT到Windows 8。

Maybe you just save your settings in Windows Registry?
That's very easy. Using SaveSeting and GetSetting is much easier than creating INI file.
And there is no trouble in compatibility, from WinNT to Windows 8.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文