@msdn=http://search.microsoft.com/search/results.aspx?qu=$$$ @pinvoke=http://pinvoke.net/$$$.htm Summary: a replacement for SHGetFolderPath !!!!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 !!!!C# Signature with marshaling: [DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = false)] static extern string SHGetKnownFolderPath( [MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken = default); !!!!VB Signature: <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]. !!!!Alternative Managed API: * The built-in System.Environment.GetFolderPath method. This method does not provide access to folders introduced since Windows Vista (like the Downloads folder): https://docs.microsoft.com/en-us/dotnet/api/system.environment.getfolderpath * The Visual Basic .NET portability API My.Computer.FileSystem.SpecialDirectories. This method does not provide access to folders introduced since Windows Vista (like the Downloads folder): https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.specialdirectories !!!!Notes: * 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. * If you use the marshaling version, which automatically throws exceptions if the method returns a failure code, the failure code to exception mapping is available here: https://docs.microsoft.com/en-us/dotnet/framework/interop/how-to-map-hresults-and-exceptions * 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); } } !!!!Sample Code with marshaling (C#): // C# public static string GetKnownFolderPath(Guid folderGuid) { return SHGetKnownFolderPath(folderGuid, 0); } !!!!Sample Code (VB.NET): ' 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 Documentation: SHGetKnownFolderPath@msdn on MSDN
Edit shell32.SHGetKnow...
You do not have permission to change this page. If you feel this is in error, please send feedback with the contact link on the main page.