SHGetFolderPath (shell32)
Last changed: -24.39.41.252

.
Summary

C# Signature:

[DllImport("shell32.dll")]
static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken,
   uint dwFlags, [Out] StringBuilder pszPath);

VB.Net Signature:

    <DllImport("shell32.dll")> _
    Private Shared Function SHGetFolderPath(ByVal hwndOwner As IntPtr, ByVal nFolder As Int32, ByVal hToken As IntPtr, ByVal dwFlags As Int32, ByVal pszPath As StringBuilder) As Int32
    End Function

C# struct for nFolder:

    public enum SpecialFolderType
    {
        // Version 5.0. The file system directory that is used
        // to store administrative tools for an individual user.
        // The Microsoft Management Console (MMC) will save customized
        // consoles to this directory, and it will roam with the user.
        AdministrativeTools = 0x0030,

        // Version 5.0. The file system directory containing
        // administrative tools for all users of the computer.
        CommonAdministrativeTools = 0x002f,

        // Version 4.71. The file system directory that serves as
        // a common repository for application-specific data.
        // A typical path is C:\Documents and Settings\username\Application Data.
        // This CSIDL is supported by the redistributable Shfolder.dll
        // for systems that do not have the Microsoft Internet Explorer 4.0
        // integrated Shell installed
        ApplicationData = 0x001a,

        // Version 5.0. The file system directory containing
        // application data for all users. A typical path is
        // C:\Documents and Settings\All Users\Application Data.
        CommonAppData = 0x0023,

        // The file system directory that contains documents
        // that are common to all users. A typical paths is
        // C:\Documents and Settings\All Users\Documents.
        // Valid for Windows NT systems and Microsoft Windows 95 and
        // Windows 98 systems with Shfolder.dll installed.
        CommonDocuments = 0x002e,

        // The file system directory that serves as a common repository
        // for Internet cookies. A typical path is
        // C:\Documents and Settings\username\Cookies.
        Cookies = 0x0021,

        // Version 5.0. Combine this CSIDL with any of the following CSIDLs
        // to force the creation of the associated folder.
        CreateFlag = 0x8000,

        // The file system directory that serves as a common repository
        // for Internet history items.
        History = 0x0022,

        // Version 4.72. The file system directory that serves as
        // a common repository for temporary Internet files. A typical
        // path is C:\Documents and Settings\username\Local Settings\Temporary Internet Files.
        InternetCache = 0x0020,

        // Version 5.0. The file system directory that serves as a data
        // repository for local (nonroaming) applications. A typical path
        // is C:\Documents and Settings\username\Local Settings\Application Data.
        LocalApplicationData = 0x001c,

        // Version 5.0. The file system directory that serves as
        // a common repository for image files. A typical path is
        // C:\Documents and Settings\username\My Documents\My Pictures.
        MyPictures = 0x0027,

        // Version 6.0. The virtual folder representing the My Documents
        // desktop item. This is equivalent to CSIDL_MYDOCUMENTS.
        // Previous to Version 6.0. The file system directory used to
        // physically store a user's common repository of documents.
        // A typical path is C:\Documents and Settings\username\My Documents.
        // This should be distinguished from the virtual My Documents folder
        // in the namespace. To access that virtual folder,
        // use SHGetFolderLocation, which returns the ITEMIDLIST for the
        // virtual location, or refer to the technique described in
        // Managing the File System.
        Personal = 0x0005,

        // Version 5.0. The Program Files folder. A typical
        // path is C:\Program Files.
        ProgramFiles = 0x0026,

        // Version 5.0. A folder for components that are shared across
        // applications. A typical path is C:\Program Files\Common.
        // Valid only for Windows NT, Windows 2000, and Windows XP systems.
        // Not valid for Windows Millennium Edition (Windows Me).
        CommonProgramFiles = 0x002b,

        // Version 5.0. The Windows System folder. A typical
        // path is C:\Windows\System32.
        System = 0x0025,

        // Version 5.0. The Windows directory or SYSROOT.
        // This corresponds to the %windir% or %SYSTEMROOT% environment
        // variables. A typical path is C:\Windows.
        Windows = 0x0024
    }

User-Defined Types:

    Private Const CSIDL_WINDOWS As Integer = &H24

Sample Code:

    Dim winPath As New StringBuilder(300)
    If SHGetFolderPath(Nothing, CSIDL_WINDOWS, Nothing, 0, winPath) <> 0 Then
        Throw New ApplicationException("Can't get window's directory")
    End If
    Console.WriteLine(winPath.ToString)

Alternative Managed API:

Do you know one? Please contribute it!

Documentation