SHGetSpecialFolderPath (shell32)
Last changed: -82.143.213.85

.
Summary

C# Signature:

  [DllImport("Shell32.dll", CharSet=CharSet.Unicode)]
   static extern int SHGetSpecialFolderPath(IntPtr hwndOwner, IntPtr lpszPath, int nFolder, int fCreate);

VB Signature:

<DllImport("Shell32.dll")> Shared Function SHGetSpecialFolderPath(ByVal hwndOwner As IntPtr,
   <Out()> ByVal lpszPath As IntPtr, ByVal nFolder As Integer, ByVal fCreate As Boolean) As Boolean
End Function

User-Defined Types:

None.

Notes:

lpszPath must be at least MAX_PATH (260) characters in size.

Tips & Tricks:

Please add some!

Sample Code:

   int CSIDL_MYMUSIC = 0x000d;
   IntPtr path = Marshal.AllocHGlobal(260 * 2);    // UNICODE
   SHGetSpecialFolderPath(parent.Handle, path, CSIDL_MYMUSIC, 0);

   string myMusicDir = Marshal.PtrToStringUni(path);
   //? Marshal.FreeHGlobal(path);

Alternative Managed API:

Environment.GetFolderPath(Environment.SpecialFolder.xxx) probably calls this internally - it does the same thing.

However not all of the enums are available (e.g. CSIDL_COMMON_DOCUMENTS missing on .NET 1.1) therefore this is needed sometimes.

Documentation

'Managed API Using Shell32.dll

(VB)

    Public Shared Function SpecialFolderPathSH32(csidl As Integer, _
   Optional ByRef DisplayName As String = "", _
    Optional ByRef TypeName As String = "") As String

        Dim path As String = ""
        Dim sh As New Shell32.Shell

        If csidl <> CInt(Environment.SpecialFolder.MyComputer) _
         AndAlso [Enum].IsDefined(GetType(Environment.SpecialFolder), csidl) Then
            Dim f2 As Shell32.Folder2 = TryCast(sh.NameSpace(17), Shell32.Folder2)
            If Not IsNothing(f2) Then
                Dim shi As Shell32.FolderItem = f2.Self
                DisplayName = shi.Name
                TypeName = shi.Type
                path = shi.Path
                shi = Nothing
            End If
            Return path
            sh = Nothing
        Else
            Dim folder As Shell32.Folder = Nothing
            If csidl = 12 Then ' !!! MYDOCUMENTS
                Dim dtf As Shell32.Folder2 = CType( _
                 sh.NameSpace(Environment.SpecialFolder.Desktop), Shell32.Folder2)
                '"::{450d8fba-ad25-11d0-98a8-0800361b1103}"
                Dim fit2 As Shell32.FolderItem2 = CType( _
                 dtf.ParseName("::{450d8fba-ad25-11d0-98a8-0800361b1103}"), Shell32.FolderItem2)
                TypeName = fit2.Type
                DisplayName = fit2.Name
                path = fit2.Path
                dtf = Nothing
                sh = Nothing
                fit2 = Nothing
                Return path
            ElseIf csidl = 17 Then ' !!! MYCOMPUTER
                Dim f2 As Shell32.Folder2 = TryCast(sh.NameSpace(17), Shell32.Folder2)
                If Not IsNothing(f2) Then
                    path = f2.Self.Path
                    DisplayName = f2.Self.Name
                    TypeName = f2.Self.Type
                    f2 = Nothing
                End If
                sh = Nothing
                Return path
            Else
                folder = sh.NameSpace(csidl)
                'End If
                Dim folder2 As Shell32.Folder2 = TryCast(folder, Shell32.Folder2)
                If Not IsNothing(folder2) Then
                    path = folder2.Self.Path
                    DisplayName = folder2.Title
                    TypeName = folder2.Self.Type
                End If
                End If
            sh = Nothing
                Return path
            End If
    End Function