Rätt eller fel forum... ingen aning! :-) VB 6.0: Wow - där fick jag att bita i! Tack för hjälpen - ska se om det blir som jag vill ha det! <code> Pillade lite i koden Min fråga hör inte hemma här igentligen men jag undrar hur får ni koden ni skriver i forumet att ha en annan bakgrundsfärg? Skriv: < code > (utan mellanslag) och < / code > glöm inte sluttaggen (som Molle antagligen skrev helt rätt på för den kom inte med)Skrivbord
Jag vill i VB (och VBA) hitta sökvägen till användarens skrivbord. Denna är ju olika beroende på operativ och språk.
Finns det något sätt att med kod ta reda på detta för just denna maskinen? Jag är dålig på registret - finns det där? Kan jag hitta värdet med kod? Eller finns det andra sätt?Sv: Skrivbord
I module:
<code>
Public Enum SHFolders
CSIDL_DESKTOP = &H0 ' <desktop>
CSIDL_INTERNET = &H1 ' Internet Explorer (icon on desktop)
CSIDL_PROGRAMS = &H2 ' Start Menu\Programs
CSIDL_CONTROLS = &H3 ' My Computer\Control Panel
CSIDL_PRINTERS = &H4 ' My Computer\Printers
CSIDL_PERSONAL = &H5 ' My Documents
CSIDL_FAVORITES = &H6 ' <user name>\Favorites
CSIDL_STARTUP = &H7 ' Start Menu\Programs\Startup
CSIDL_RECENT = &H8 ' <user name>\Recent
CSIDL_SENDTO = &H9 ' <user name>\SendTo
CSIDL_BITBUCKET = &HA ' <desktop>\Recycle Bin
CSIDL_STARTMENU = &HB ' <user name>\Start Menu
CSIDL_MYDOCUMENTS = &HC ' logical "My Documents" desktop icon
CSIDL_MYMUSIC = &HD ' "My Music" folder
CSIDL_MYVIDEO = &HE ' "My Videos" folder
CSIDL_DESKTOPDIRECTORY = &H10 ' <user name>\Desktop
CSIDL_DRIVES = &H11 ' My Computer
CSIDL_NETWORK = &H12 ' Network Neighborhood (My Network Places)
CSIDL_NETHOOD = &H13 ' <user name>\nethood
CSIDL_FONTS = &H14 ' windows\fonts
CSIDL_TEMPLATES = &H15
CSIDL_COMMON_STARTMENU = &H16 ' All Users\Start Menu
CSIDL_COMMON_PROGRAMS = &H17 ' All Users\Start Menu\Programs
CSIDL_COMMON_STARTUP = &H18 ' All Users\Startup
CSIDL_COMMON_DESKTOPDIRECTORY = &H19 ' All Users\Desktop
CSIDL_APPDATA = &H1A ' <user name>\Application Data
CSIDL_PRINTHOOD = &H1B ' <user name>\PrintHood
CSIDL_LOCAL_APPDATA = &H1C ' <user name>\Local Settings\Applicaiton Data (non roaming)
CSIDL_ALTSTARTUP = &H1D ' non localized startup
CSIDL_COMMON_ALTSTARTUP = &H1E ' non localized common startup
CSIDL_COMMON_FAVORITES = &H1F
CSIDL_INTERNET_CACHE = &H20
CSIDL_COOKIES = &H21
CSIDL_HISTORY = &H22 ' IE History folder
CSIDL_COMMON_APPDATA = &H23 ' All Users\Application Data
CSIDL_WINDOWS = &H24 ' GetWindowsDirectory()
CSIDL_SYSTEM = &H25 ' GetSystemDirectory()
CSIDL_PROGRAM_FILES = &H26 ' C:\Program Files
CSIDL_MYPICTURES = &H27 ' C:\Program Files\My Pictures
CSIDL_PROFILE = &H28 ' USERPROFILE
CSIDL_SYSTEMX86 = &H29 ' x86 system directory on RISC
CSIDL_PROGRAM_FILESX86 = &H2A ' x86 C:\Program Files on RISC
CSIDL_PROGRAM_FILES_COMMON = &H2B ' C:\Program Files\Common
CSIDL_PROGRAM_FILES_COMMONX86 = &H2C ' x86 Program Files\Common on RISC
CSIDL_COMMON_TEMPLATES = &H2D ' All Users\Templates
CSIDL_COMMON_DOCUMENTS = &H2E ' All Users\Documents
CSIDL_COMMON_ADMINTOOLS = &H2F ' All Users\Start Menu\Programs\Administrative Tools
CSIDL_ADMINTOOLS = &H30 ' <user name>\Start Menu\Programs\Administrative Tools
CSIDL_CONNECTIONS = &H31 ' Network and Dial-up Connections
CSIDL_COMMON_MUSIC = &H35 ' All Users\My Music
CSIDL_COMMON_PICTURES = &H36 ' All Users\My Pictures
CSIDL_COMMON_VIDEO = &H37 ' All Users\My Video
CSIDL_RESOURCES = &H38 ' Resource Direcotry
CSIDL_COMMON_OEM_LINKS = &H3A ' Links to All Users OEM specific apps
CSIDL_CDBURN_AREA = &H3B ' USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
CSIDL_COMPUTERSNEARME = &H3D ' Computers Near Me (computered from Workgroup membership)
End Enum
Public Declare Function SHGetPathFromIDList Lib "shell32" _
Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) As Long
Public Declare Function SHGetSpecialFolderLocation Lib "shell32" _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
pidl As Long) As Long
Public Function GetSpecialFolder(CSIDLx As SHFolders, hwndOwner As Long) As String
Dim lngTemp As Long
dim CSIDL as long
CSIDL = clng(CSIDLx)
lngTemp = hwndOwner
'a few local variables needed
Dim sPath As String
Dim pidl As Long
Const ERROR_SUCCESS = 0
Const MAX_LENGTH = 260
'fill pidl with the specified folder item
If SHGetSpecialFolderLocation(lngTemp, CSIDL, pidl) = ERROR_SUCCESS Then
'Of the structure is filled, initialize and
'retrieve the path from the id list, and return
'the folder with a trailing slash appended.
sPath = Space$(MAX_LENGTH)
If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then
GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1) & "\"
End If
End If
End Function
</code>
Det här borde fungera... Jag vet inte var jag hittade koden, men någonstanns var det iaf. :)Sv: Skrivbord
:-)Sv: Skrivbord
msgbox "Skrivbordet: " & GetSpecialFolder(SHFolders.CSIDL_DESKTOP, EttFönster.hwnd)
</code>
Det border fungera
Observera att jag har gjort om koden lite för att göra den presentabel, hoppas att den inte slutade att fungera bara... ;)Sv: Skrivbord
Testa så skall det funka
//Tobbe
<code>
Option Explicit
Public Enum SHFolders
CSIDL_DESKTOP = &H0 ' <desktop>
CSIDL_INTERNET = &H1 ' Internet Explorer (icon on desktop)
CSIDL_PROGRAMS = &H2 ' Start Menu\Programs
CSIDL_CONTROLS = &H3 ' My Computer\Control Panel
CSIDL_PRINTERS = &H4 ' My Computer\Printers
CSIDL_PERSONAL = &H5 ' My Documents
CSIDL_FAVORITES = &H6 ' <user name>\Favorites
CSIDL_STARTUP = &H7 ' Start Menu\Programs\Startup
CSIDL_RECENT = &H8 ' <user name>\Recent
CSIDL_SENDTO = &H9 ' <user name>\SendTo
CSIDL_BITBUCKET = &HA ' <desktop>\Recycle Bin
CSIDL_STARTMENU = &HB ' <user name>\Start Menu
CSIDL_MYDOCUMENTS = &HC ' logical "My Documents" desktop icon
CSIDL_MYMUSIC = &HD ' "My Music" folder
CSIDL_MYVIDEO = &HE ' "My Videos" folder
CSIDL_DESKTOPDIRECTORY = &H10 ' <user name>\Desktop
CSIDL_DRIVES = &H11 ' My Computer
CSIDL_NETWORK = &H12 ' Network Neighborhood (My Network Places)
CSIDL_NETHOOD = &H13 ' <user name>\nethood
CSIDL_FONTS = &H14 ' windows\fonts
CSIDL_TEMPLATES = &H15
CSIDL_COMMON_STARTMENU = &H16 ' All Users\Start Menu
CSIDL_COMMON_PROGRAMS = &H17 ' All Users\Start Menu\Programs
CSIDL_COMMON_STARTUP = &H18 ' All Users\Startup
CSIDL_COMMON_DESKTOPDIRECTORY = &H19 ' All Users\Desktop
CSIDL_APPDATA = &H1A ' <user name>\Application Data
CSIDL_PRINTHOOD = &H1B ' <user name>\PrintHood
CSIDL_LOCAL_APPDATA = &H1C ' <user name>\Local Settings\Applicaiton Data (non roaming)
CSIDL_ALTSTARTUP = &H1D ' non localized startup
CSIDL_COMMON_ALTSTARTUP = &H1E ' non localized common startup
CSIDL_COMMON_FAVORITES = &H1F
CSIDL_INTERNET_CACHE = &H20
CSIDL_COOKIES = &H21
CSIDL_HISTORY = &H22 ' IE History folder
CSIDL_COMMON_APPDATA = &H23 ' All Users\Application Data
CSIDL_WINDOWS = &H24 ' GetWindowsDirectory()
CSIDL_SYSTEM = &H25 ' GetSystemDirectory()
CSIDL_PROGRAM_FILES = &H26 ' C:\Program Files
CSIDL_MYPICTURES = &H27 ' C:\Program Files\My Pictures
CSIDL_PROFILE = &H28 ' USERPROFILE
CSIDL_SYSTEMX86 = &H29 ' x86 system directory on RISC
CSIDL_PROGRAM_FILESX86 = &H2A ' x86 C:\Program Files on RISC
CSIDL_PROGRAM_FILES_COMMON = &H2B ' C:\Program Files\Common
CSIDL_PROGRAM_FILES_COMMONX86 = &H2C ' x86 Program Files\Common on RISC
CSIDL_COMMON_TEMPLATES = &H2D ' All Users\Templates
CSIDL_COMMON_DOCUMENTS = &H2E ' All Users\Documents
CSIDL_COMMON_ADMINTOOLS = &H2F ' All Users\Start Menu\Programs\Administrative Tools
CSIDL_ADMINTOOLS = &H30 ' <user name>\Start Menu\Programs\Administrative Tools
CSIDL_CONNECTIONS = &H31 ' Network and Dial-up Connections
CSIDL_COMMON_MUSIC = &H35 ' All Users\My Music
CSIDL_COMMON_PICTURES = &H36 ' All Users\My Pictures
CSIDL_COMMON_VIDEO = &H37 ' All Users\My Video
CSIDL_RESOURCES = &H38 ' Resource Direcotry
CSIDL_COMMON_OEM_LINKS = &H3A ' Links to All Users OEM specific apps
CSIDL_CDBURN_AREA = &H3B ' USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
CSIDL_COMPUTERSNEARME = &H3D ' Computers Near Me (computered from Workgroup membership)
End Enum
Private Declare Function SHGetPathFromIDList Lib "shell32" _
Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) As Long
Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
pidl As Long) As Long
Public Function GetSpecialFolder(CSIDLx As SHFolders, hwndOwner As Long) As String
Dim lngTemp As Long
Dim CSIDL As Long
CSIDL = CLng(CSIDLx)
lngTemp = hwndOwner
'a few local variables needed
Dim sPath As String
Dim pidl As Long
Const ERROR_SUCCESS = 0
Const MAX_LENGTH = 260
'fill pidl with the specified folder item
If SHGetSpecialFolderLocation(lngTemp, CSIDL, pidl) = ERROR_SUCCESS Then
'Of the structure is filled, initialize and
'retrieve the path from the id list, and return
'the folder with a trailing slash appended.
sPath = Space$(MAX_LENGTH)
If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then
GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1) & "\"
End If
End If
End Function
Private Sub Form_Load()
MsgBox "Skrivbordet: " & GetSpecialFolder(SHFolders.CSIDL_DESKTOP, Form1.hWnd)
End Sub
</code>Sv: Skrivbord
<code>
Jag testar
</code>
<code>
Oki funkar tack för det
</code>Sv: Skrivbord
Blir:
<code>
Option explicit
'Hur fungerar det här?
</code>
EDIT: Fixade sluttagget ;)Sv: Skrivbord
< /code >
som sagt -utan mellanslag den också. =)
/Emma