Results 1 to 6 of 6

Thread: [RESOLVED] GetSpecialFolder Problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Resolved [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:
    1. Option Explicit
    2.  
    3. Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _
    4.  (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
    5. Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" _
    6.  (ByVal pidl As Long, ByVal pszPath As String) As Long
    7.  
    8. Private Const ERROR_SUCCESS = 0
    9. Private Const MAX_LENGTH = 260
    10. 'Private Const CSIDL_COMMON_DOCUMENTS = &H2E
    11. Private Const CSIDL_PERSONAL = &H5
    12.  
    13. Public Function GetSpecialFolder() As String
    14. Dim sPath As String
    15. Dim pidl As Long
    16.  
    17.   'Get the ID list from the Path ID
    18.   If SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, pidl) = ERROR_SUCCESS Then
    19.     'Allocate the space for the path
    20.     sPath = Space$(MAX_LENGTH)
    21.     'Get the real path from the ID list
    22.     If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then
    23.       'Strip off the trailing null characters
    24.       GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1)
    25.       'And add a trailing \
    26.       If Right(GetSpecialFolder, 1) <> "\" Then GetSpecialFolder = GetSpecialFolder & "\"
    27.     End If
    28.   End If
    29. End Function

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    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?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: GetSpecialFolder Problem

    here is Allapi.net Example

    VB Code:
    1. Const CSIDL_DESKTOP = &H0
    2. Const CSIDL_PROGRAMS = &H2
    3. Const CSIDL_CONTROLS = &H3
    4. Const CSIDL_PRINTERS = &H4
    5. Const CSIDL_PERSONAL = &H5
    6. Const CSIDL_FAVORITES = &H6
    7. Const CSIDL_STARTUP = &H7
    8. Const CSIDL_RECENT = &H8
    9. Const CSIDL_SENDTO = &H9
    10. Const CSIDL_BITBUCKET = &HA
    11. Const CSIDL_STARTMENU = &HB
    12. Const CSIDL_DESKTOPDIRECTORY = &H10
    13. Const CSIDL_DRIVES = &H11
    14. Const CSIDL_NETWORK = &H12
    15. Const CSIDL_NETHOOD = &H13
    16. Const CSIDL_FONTS = &H14
    17. Const CSIDL_TEMPLATES = &H15
    18. Const MAX_PATH = 260
    19. Private Type ****EMID
    20.     cb As Long
    21.     abID As Byte
    22. End Type
    23. Private Type ITEMIDLIST
    24.     mkid As ****EMID
    25. End Type
    26. 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
    27. Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    28. Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    29. Private Sub Form_Load()
    30.     'KPD-Team 1998
    31.     'URL: [url]http://www.allapi.net/[/url]
    32.     'E-Mail: [email][email protected][/email]
    33.     'Show an about window
    34.     ShellAbout Me.hWnd, App.Title, "Created by the KPD-Team 1999", ByVal 0&
    35.     'Set the graphical mode to persistent
    36.     Me.AutoRedraw = True
    37.     'Print the folders to the form
    38.     Me.Print "Start menu folder: " + GetSpecialfolder(CSIDL_STARTMENU)
    39.     Me.Print "Favorites folder: " + GetSpecialfolder(CSIDL_FAVORITES)
    40.     Me.Print "Programs folder: " + GetSpecialfolder(CSIDL_PROGRAMS)
    41.     Me.Print "Desktop folder: " + GetSpecialfolder(CSIDL_DESKTOP)
    42. End Sub
    43. Private Function GetSpecialfolder(CSIDL As Long) As String
    44.     Dim r As Long
    45.     Dim IDL As ITEMIDLIST
    46.     'Get the special folder
    47.     r = SHGetSpecialFolderLocation(100, CSIDL, IDL)
    48.     If r = NOERROR Then
    49.         'Create a buffer
    50.         Path$ = Space$(512)
    51.         'Get the path from the IDList
    52.         r = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal Path$)
    53.         'Remove the unnecessary chr$(0)'s
    54.         GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1)
    55.         Exit Function
    56.     End If
    57.     GetSpecialfolder = ""
    58. End Function

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width