[RESOLVED] GetSpecialFolder Problem
I am trying to use an api to get the path to the my documuments folder. On my machine it works very well. However, when I test it on a machine that has the My Documents folder on a different drive, it fails.
Here is the code. I got it off line, possibly here, or another site. I looked at a bunch of places.
Anyone know how I can compensate for if the My Documents folder is on a different drive? I have looked around on google and here and haven't come accross anything that addresses that situation.
Thank you for your help.
VB Code:
Option Explicit
Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, ByVal pszPath As String) As Long
Private Const ERROR_SUCCESS = 0
Private Const MAX_LENGTH = 260
'Private Const CSIDL_COMMON_DOCUMENTS = &H2E
Private Const CSIDL_PERSONAL = &H5
Public Function GetSpecialFolder() As String
Dim sPath As String
Dim pidl As Long
'Get the ID list from the Path ID
If SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, pidl) = ERROR_SUCCESS Then
'Allocate the space for the path
sPath = Space$(MAX_LENGTH)
'Get the real path from the ID list
If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then
'Strip off the trailing null characters
GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1)
'And add a trailing \
If Right(GetSpecialFolder, 1) <> "\" Then GetSpecialFolder = GetSpecialFolder & "\"
End If
End If
End Function
Re: GetSpecialFolder Problem
Is it redirected to another local drive or a network drive on another system like a server? Usually roaming profiles will have the my documents folder located on the server.
Re: GetSpecialFolder Problem
Thank you for responding to my question.
There are two physical drives on the computer. I am dealing with public schools, and often they will put the os on the primary drive, then put documents and settings (for xp) on the slave drive. This is in a lot of cases, but not all, so I have to be able to find the My Documents folder on the local PC. I am not aware of any times that they use roaming profiles, though I suppose it is possible. On the test machine the my documents folder is on the slave drive.
The wierd thing is that inno correctly installs the necessary files in the my documents folder, but my application can't find the My documents path to get to them.
This is probably too much information, but I am trying to answer your question completly. Is it still possible to get to the my document folder with this api if it is on the slave drive? If so, how?
Re: GetSpecialFolder Problem
AFAIK, it should get the location but I'm thinking how the My Documents folder is "remapped". If its folder path is set by right clicking on the my documents default folder under th users profile on the C drive then it should work. If the my documents folder is moved by a cut/paste operationg or something then I would expect it to fail.
Yes, strange that Inno is finding it but your app is not.
Re: GetSpecialFolder Problem
here is Allapi.net Example
VB Code:
Const CSIDL_DESKTOP = &H0
Const CSIDL_PROGRAMS = &H2
Const CSIDL_CONTROLS = &H3
Const CSIDL_PRINTERS = &H4
Const CSIDL_PERSONAL = &H5
Const CSIDL_FAVORITES = &H6
Const CSIDL_STARTUP = &H7
Const CSIDL_RECENT = &H8
Const CSIDL_SENDTO = &H9
Const CSIDL_BITBUCKET = &HA
Const CSIDL_STARTMENU = &HB
Const CSIDL_DESKTOPDIRECTORY = &H10
Const CSIDL_DRIVES = &H11
Const CSIDL_NETWORK = &H12
Const CSIDL_NETHOOD = &H13
Const CSIDL_FONTS = &H14
Const CSIDL_TEMPLATES = &H15
Const MAX_PATH = 260
Private Type ****EMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As ****EMID
End Type
Private Declare Function ShellAbout Lib "shell32.dll" Alias "ShellAboutA" (ByVal hWnd As Long, ByVal szApp As String, ByVal szOtherStuff As String, ByVal hIcon As Long) As Long
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: [url]http://www.allapi.net/[/url]
'Show an about window
ShellAbout Me.hWnd, App.Title, "Created by the KPD-Team 1999", ByVal 0&
'Set the graphical mode to persistent
Me.AutoRedraw = True
'Print the folders to the form
Me.Print "Start menu folder: " + GetSpecialfolder(CSIDL_STARTMENU)
Me.Print "Favorites folder: " + GetSpecialfolder(CSIDL_FAVORITES)
Me.Print "Programs folder: " + GetSpecialfolder(CSIDL_PROGRAMS)
Me.Print "Desktop folder: " + GetSpecialfolder(CSIDL_DESKTOP)
End Sub
Private Function GetSpecialfolder(CSIDL As Long) As String
Dim r As Long
Dim IDL As ITEMIDLIST
'Get the special folder
r = SHGetSpecialFolderLocation(100, CSIDL, IDL)
If r = NOERROR Then
'Create a buffer
Path$ = Space$(512)
'Get the path from the IDList
r = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal Path$)
'Remove the unnecessary chr$(0)'s
GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1)
Exit Function
End If
GetSpecialfolder = ""
End Function
Re: GetSpecialFolder Problem
I solved the problem. I isolated the api in a test project and tested it on the machine. It returned the right results. After searching around I found that the name function doesn't work so well when moving across drives where the file structure is not the same. I can't test until the am, but I am confident that is the problem. I used filecopy instead so I am closing this, for now.