Option Explicit
Private Declare Function SHGetSpecialFolderPathW Lib "shell32" (ByVal hWnd As Long, ByVal lpszPath As Long, ByVal nFolder As Long, ByVal fCreate As Long) As Boolean
Private Declare Function PathAppendW Lib "shlwapi" (ByVal pszPath As Long, ByVal pszMore As Long) As Long
Private Const CSIDL_COMMON_APPDATA = 35
Private Const CSIDL_LOCAL_APPDATA = 28
Private Function GetRawBuffer(ByVal lpBuffer As String) As String
If LenB(lpBuffer) = 0 Then
Exit Function ' _leave
End If
If InStr(lpBuffer, vbNullChar) Then
GetRawBuffer = Left$(lpBuffer, InStr(lpBuffer, vbNullChar) - 1)
Else
GetRawBuffer = lpBuffer
End If
End Function
Public Function AllUserAppData(Optional szAppend As String = vbNullString) As String
Dim Buffer(4096 - 1) As Byte
' Attempt to get the (application all user data) folder location.
If SHGetSpecialFolderPathW(0, VarPtr(Buffer(0)), CSIDL_COMMON_APPDATA, 0) Then
' Check that we want to append data to this location.
If LenB(szAppend) <> 0 Then
' We want to append some data to this location.
If PathAppendW(VarPtr(Buffer(0)), StrPtr(szAppend)) Then
AllUserAppData = GetRawBuffer(Buffer)
Else
AllUserAppData = vbNullString
End If
Else
' We just want the location only.
AllUserAppData = GetRawBuffer(Buffer)
End If
Else
' The function failed.
MsgBox "SHGetSpecialFolderPathW failed. Error= " & Err.LastDllError
End If
' Free memory.
Erase Buffer
End Function
Private Sub Form_Load()
MsgBox AllUserAppData("\AppName\yourfiles.txt")
End Sub
http://www.andreavb.com/forum/viewtopic_4543.html