Get The Desktop Path

'Insert this code to your form:

Private Const ERROR_SUCCESS = 0&
Private Const HKEY_CURRENT_USER = &H80000001
Private Const SYNCHRONIZE = &H100000
Private Const READ_CONTROL = &H20000
Private Const STANDARD_RIGHTS_READ = READ_CONTROL
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or _
KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Const REG_SZ = 1
Private Declare Function RegCloseKey& Lib "ADVAPI32.DLL" (ByVal hKey&)
Private Declare Function RegOpenKeyExA& Lib "ADVAPI32.DLL" _
(ByVal hKey&, ByVal lpSubKey$, ByVal ulOptions&, _
ByVal samDesired&, phkResult&)
Private Declare Function RegQueryValueExA& Lib "ADVAPI32.DLL" (ByVal _
hKey&, ByVal lpValueName$, ByVal lpReserved&, lpType&, lpData As Any, _
lpcbData&)

Private Function sGetDesktop() As String
Const nLG As Long = 256
Dim sValue As String * nLG
Dim hKey As Long
Dim nType As Long
Dim nCR As Long
If (RegOpenKeyExA(HKEY_CURRENT_USER, _
"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", 0, _
KEY_READ, hKey) = ERROR_SUCCESS) Then
If (RegQueryValueExA(hKey, "Desktop", 0, nType, ByVal sValue, nLG) _
= ERROR_SUCCESS) Then
If (nType = REG_SZ) Then
sGetDesktop = Left(sValue, InStr(sValue, vbNullChar) - 1)
End If
End If
nCR = RegCloseKey(hKey)
End If
End Function

Private Sub Form_Load()
MsgBox "Your desktop path is " & sGetDesktop
End Sub

Go Back