views:

82

answers:

3

Basically the same as this question, but for VB6.

A customer's application "AppName" has its configuration files stored in CommonAppData.

  • Under Windows XP that is C:\Documents and Settings\All Users\Application Data\AppName
  • Under Windows Vista that is C:\ProgramData\AppName

How do I get the correct foldername with VB6??

Additional notes, I prefer to use a API Call instead of adding a reference to the shell32.dll

+1  A: 

found it;

Private Declare Function SHGetFolderPath _
                        Lib "shfolder.dll" Alias "SHGetFolderPathA" _
                        (ByVal hwndOwner As Long, _
                         ByVal nFolder As Long, _
                         ByVal hToken As Long, _
                         ByVal dwReserved As Long, _
                         ByVal lpszPath As String) As Long
Private Const CSIDL_COMMON_APPDATA = &H23
Private Const CSIDL_COMMON_DOCUMENTS = &H2E

Public Function strGetCommonAppDataPath() As String
    Dim strPath As String

    strPath = Space$(512)
    Call SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, 0, strPath)
    strPath = Left$(strPath, InStr(strPath, vbNullChar))

    strGetCommonAppDataPath = strPath
End Function
DavRob60
+1  A: 

Karl Peterson has published a drop-in VB6 class called CSystemFolders that will find CSIDL_APPDATA, CSIDL_LOCAL_APPDATA and CSIDL_COMMON_APPDATA.

Karl's code is always reliable, accept no substitutes :)

MarkJ
Karl hasn't thought it through. That's a lot of baggage for something so trivial.
Bob Riemersma
@Bob +1 to your answer that uses the shell object.
MarkJ
I'm a Karl fan too, but I think he goes overboard at times. Something like this is usually called only a very few times in the lifetime of a program, so performance isn't really an issue. Also, the Shell32 COM interface didn't maintain binary compatibility across all Windows versions so late binding is a good idea here.
Bob Riemersma
+3  A: 

Use late binding:

Const ssfCOMMONAPPDATA = &H23
Dim strCommonAppData As String

strCommonAppData = _
    CreateObject("Shell.Application").NameSpace(ssfCOMMONAPPDATA).Self.Path
Bob Riemersma