Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than shell32, prefix the name with the module name and a period.
SHGetKnownFolderPath (shell32)
.
C# Signature:
[DllImport("shell32.dll")]
static extern int SHGetKnownFolderPath(
[MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
uint dwFlags,
IntPtr hToken,
out IntPtr ppszPath); // must be freed with Marshal.FreeCoTaskMem
<DllImport("shell32.dll")> _
Shared Function SHGetKnownFolderPath(
<MarshalAs(UnmanagedType.LPStruct)> ByVal rfid As Guid,
ByVal dwFlags As UInteger,
ByVal hToken As IntPtr,
ByRef ppszPath As IntPtr ' must be freed with Marshal.FreeCoTaskMem
) As Integer
End Function
VB Signature with marshaling:
<DllImport("shell32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True, PreserveSig:=False)>
Shared Function SHGetKnownFolderPath(
<MarshalAs(UnmanagedType.LPStruct)> ByVal rfid As Guid,
ByVal dwFlags As UInteger,
ByVal hToken As IntPtr
) As String
End Function
User-Defined Types:
The list of values for the rfid Guid are available on KNOWNFOLDERID.
If you do not use the marshaling version, you convert the returned ppszPath to a string with Marshal.PtrStringToUni. You must always call Marshal.FreeCoTaskMem on it afterwards, even if the method returns a failure code.
Not all folders are available on every system (for example, the playlists folder). Check the returned failure code or exception.
This returns the internal / actual file system path. The folder name displayed in the File Explorer may be localized, e.g. the Playlists folder showing as "Musikwiedergabelisten" on a German system. To retrieve the localized name, instantiate an IShellItem with SHGetKnownFolderItem, and call its GetDisplayName method.
Sample Code (C#):
// C#
using System.Runtime.InteropServices;
public static string? GetKnownFolderPath(Guid folderGuid)
{
IntPtr ppszPath = default;
try
{
int hr = SHGetKnownFolderPath(folderGuid, 0, IntPtr.Zero, out ppszPath);
Marshal.ThrowExceptionForHR(hr); // alternatively, check success with hr >= 0
return Marshal.PtrToStringUni(ppszPath);
}
finally
{
Marshal.FreeCoTaskMem(ppszPath);
}
}
' VB.NET
Public Shared Function GetKnownFolderPath(ByVal folderGuid As Guid) As String
Dim ppszPath As IntPtr
Try
Dim hr As Integer = SHGetKnownFolderPath(folderGuid, 0, IntPtr.Zero, ppszPath)
Marshal.ThrowExceptionForHR(hr) ' alternatively, check success with hr >= 0
Return Marshal.PtrToStringUni(ppszPath)
Finally
Marshal.FreeCoTaskMem(ppszPath)
End Try
End Function
Sample Code with marshaling (VB.NET):
' VB.NET
Public Shared Function GetKnownFolderPath(ByVal folderGuid As Guid) As String
Return SHGetKnownFolderPath(folderGuid, 0, IntPtr.Zero)
End Function
The SHGetFolderPath API
1/17/2013 11:06:46 PM - -24.39.41.252
The KNOWNFOLDERID constants represent GUIDs that identify standard folders registered with the system as Known Folders. These folders are installed with Windows Vista and later operating systems, and a computer will have only folders appropriate to it installed.
4/21/2022 12:59:51 PM - Ray-62.214.243.122
Exposes methods that retrieve information about a Shell item. IShellItem and IShellItem2 are the preferred representations of items in any new code
3/3/2021 1:24:20 PM - -63.118.230.241
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).