我应该在哪里存储应用程序特定的设置? [英] Where should I store application specific settings?

查看:115
本文介绍了我应该在哪里存储应用程序特定的设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

在WinXP上,我有以下内容:

On WinXP, I have the following:

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

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

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

有2个问题:

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

推荐答案

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

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

  • CSIDL_APPDATA文件夹是您可能最感兴趣的文件夹.在此存储的数据可供漫游用户在其登录到的任何计算机上使用.这是存储简单配置数据的最佳位置.所有用户都对此文件夹(和最后一个文件夹)具有写权限.请注意,以上文件夹都不是用户生成的数据!那将正确地属于我的文档"层次结构.
  • 正如Cody Gray在评论中建议的那样,对于始终位于当前计算机本地但仍按每个用户保留的应用程序数据,也请考虑CSIDL_LOCAL_APPDATA.该文件夹中的数据无法漫游,因此应该是用户登录到另一台计算机时可能不会错过的数据.
  • 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.
  • 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.

我无耻地从卡尔·彼得森(Karl Peterson)的好文章中复制了上面的解释,解释这对于VB6程序员而言. Karl还有一个即用型可以帮助您找到目录,但是恕我直言这次他太复杂了. Bob Riemersma可以使用

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 

我认为继续使用是可以的这些目录中的INI文件.

这篇关于我应该在哪里存储应用程序特定的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆