Special folder atau folder khusus adalah folder yang digunakan Windows untuk menyimpan data tertentu, misalnya folder My Documents, My Music, Favorites, History, Recent, dll. Berikut ini adalah kode fungsi untuk mengetahui lokasi folder-folder tersebut.
[ VB 6.0 ]
Buat Module baru dan ketikkan :
Private Declare Function SHGetSpecialFolderLocation Lib "shell32" (ByVal hwnd As Long, ByVal nFolder As Long, Pidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal Pidl As Long, ByVal FolderPath As String) As Long
Enum SpecialFolderConst
sfAppData = 26
sfCDBurning = 59
sfCookies = 33
sfDesktop = 0
sfFavorites = 6
sfFonts = 20
sfHistory = 34
sfLocalAppData = 28
sfMyDocuments = 5
sfMyMusic = 13
sfMyPictures = 39
sfMyVideo = 14
sfNetHood = 19
sfPrintHood = 27
sfProfile = 40
sfProgramFiles = 38
sfRecent = 8
sfSendTo = 9
sfStartMenu = 11
sfStartMenuPrograms = 2
sfStartUp = 7
sfSystem = 37
sfTempInternet = 32
sfTemplates = 21
sfWindows = 36
End Enum
Public Function SpecialFolder(ByVal SFConst As SpecialFolderConst) As String
Dim Pidl As Long
Dim s As String * 260
Dim l As Long
l = SHGetSpecialFolderLocation(0, SFConst, Pidl)
If l = 0 Then
l = SHGetPathFromIDList(Pidl, s)
If l = 1 Then
s = Left(Trim(s), InStr(s, Chr(0)) - 1)
SpecialFolder = Trim(s)
End If
End If
End Function
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal Pidl As Long, ByVal FolderPath As String) As Long
Enum SpecialFolderConst
sfAppData = 26
sfCDBurning = 59
sfCookies = 33
sfDesktop = 0
sfFavorites = 6
sfFonts = 20
sfHistory = 34
sfLocalAppData = 28
sfMyDocuments = 5
sfMyMusic = 13
sfMyPictures = 39
sfMyVideo = 14
sfNetHood = 19
sfPrintHood = 27
sfProfile = 40
sfProgramFiles = 38
sfRecent = 8
sfSendTo = 9
sfStartMenu = 11
sfStartMenuPrograms = 2
sfStartUp = 7
sfSystem = 37
sfTempInternet = 32
sfTemplates = 21
sfWindows = 36
End Enum
Public Function SpecialFolder(ByVal SFConst As SpecialFolderConst) As String
Dim Pidl As Long
Dim s As String * 260
Dim l As Long
l = SHGetSpecialFolderLocation(0, SFConst, Pidl)
If l = 0 Then
l = SHGetPathFromIDList(Pidl, s)
If l = 1 Then
s = Left(Trim(s), InStr(s, Chr(0)) - 1)
SpecialFolder = Trim(s)
End If
End If
End Function
[ VB .NET ]
Dalam VB.NET sudah terdapat fungsi untuk mengetahui lokasi Special Folder, contoh kodenya (mengetahui lokasi folder My Documents) adalah :
s = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Tapi jika fungsi diatas kurang memadai, maka harus dibuat fungsi buatan sendiri seperti berikut :Buat Module baru dan ketikkan :
Private Declare Function SHELL32_GetFolderPath Lib "shell32.dll" Alias "SHGetFolderPathA" (ByVal hwndOwner As Integer, ByVal nFolder As Integer, ByVal hToken As Integer, ByVal dwFlags As Integer, ByVal lpszPath As String) As Integer
Enum SpecialFolderConst
sfAppData = 26
sfCDBurning = 59
sfCookies = 33
sfDesktop = 0
sfFavorites = 6
sfFonts = 20
sfHistory = 34
sfLocalAppData = 28
sfMyDocuments = 5
sfMyMusic = 13
sfMyPictures = 39
sfMyVideo = 14
sfNetHood = 19
sfPrintHood = 27
sfProfile = 40
sfProgramFiles = 38
sfRecent = 8
sfSendTo = 9
sfStartMenu = 11
sfStartMenuPrograms = 2
sfStartUp = 7
sfSystem = 37
sfTempInternet = 32
sfTemplates = 21
sfWindows = 36
End Enum
Public Function SpecialFolder(ByVal SFConst As SpecialFolderConst) As String
Dim s As String = Space(260)
Dim i As Integer
i = SHELL32_GetFolderPath(0, SFConst, 0, 0, s)
If i = 0 Then
i = InStr(s, vbNullChar)
Return IIf(i > 0, Left(s, i - 1), s)
Else
Return ""
End If
End Function
Enum SpecialFolderConst
sfAppData = 26
sfCDBurning = 59
sfCookies = 33
sfDesktop = 0
sfFavorites = 6
sfFonts = 20
sfHistory = 34
sfLocalAppData = 28
sfMyDocuments = 5
sfMyMusic = 13
sfMyPictures = 39
sfMyVideo = 14
sfNetHood = 19
sfPrintHood = 27
sfProfile = 40
sfProgramFiles = 38
sfRecent = 8
sfSendTo = 9
sfStartMenu = 11
sfStartMenuPrograms = 2
sfStartUp = 7
sfSystem = 37
sfTempInternet = 32
sfTemplates = 21
sfWindows = 36
End Enum
Public Function SpecialFolder(ByVal SFConst As SpecialFolderConst) As String
Dim s As String = Space(260)
Dim i As Integer
i = SHELL32_GetFolderPath(0, SFConst, 0, 0, s)
If i = 0 Then
i = InStr(s, vbNullChar)
Return IIf(i > 0, Left(s, i - 1), s)
Else
Return ""
End If
End Function
Untuk contoh penggunannya (My Documents) ketikkan :
s = SpecialFolder(sfMyDocuments)
Source : PutraVB
EmoticonEmoticon