7
votes

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

3
Eh? Anything in Shell32.dll is an API call.MarkJ
@ MarkJ: I was saying that I don't want to add shell32.dll in the references (under the project menu).DavRob60
You mean you don't want to use the VBScript code you originally linked, because you don't want to add any references? But you don't have to add any references, you can use late-binding. Like in Bob's answer stackoverflow.com/questions/3054802/commonappdata-in-vb6/…MarkJ
I did not know about late-binding. I was under the impression that I was not able to do this because I did now know witch types to use to declare the objShell, objFolder and objFolderItem variables. Good to know!DavRob60
Yes, simply declare them As Object and all is well.Bob77

3 Answers

9
votes

Use late binding:

Const ssfCOMMONAPPDATA = &H23
Dim strCommonAppData As String

strCommonAppData = _
    CreateObject("Shell.Application").NameSpace(ssfCOMMONAPPDATA).Self.Path
3
votes

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
2
votes

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 :)