Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Accessing All of Windows Special Folders

4.96/5 (14 votes)
12 Mar 2008CPOL10 min read 2   4.6K  
Get the path names to Windows special folders; optionally create the folders if they are missing; access physical and virtual special folders.

Abstract

This article is about accessing Windows special folders. These folders include your “Favorites”, “Cookies”, system libraries and the like. Here is code, including a large number of constant definitions, plus documentation, allowing access to and creation of these folders. DotNET provides access (I think) to nine “Special Directories”, whereas the facilities contained here allow access to 49 additional “Special Folders”.

Contents

Introduction. 1

The Download. 1

Code Documentation. 2

Notes. 6

History. 6

References. 6

Tables

CSIDL Constants (SHGetFolderPathA) 6

CSIDL Flags (SHGetFolderPathA) 6

dotNET Special Directory methods. 6

Download description. 6

Special Folder listing output (Author's machine) 6

Introduction

There is no rocket science here, just a sharing of what is mainly tedious documentation. I may have missed it, but for some reason dotNET does not seem to provide extensive access to the many system directories, or special folders that are parts of so much of our Windows activity. I presume that will be corrected in some future release if it hasn’t been already. There are only nine special directories that I can find (refer to table “dotNET SpecialDirectories Class shared methods”); whereas the WinAPI provides access to those and another 49 (refer to table “WinAPI function SHGetFolderPathA CSIDL Constants”). Each of the Windows Special Folders is accessed via an integer constant and the definition of the 58 constants with associated descriptive documentation is a tedious assignment. I’ve done it and present it here for your future file-fest.

The code is essentially Windows XP, however, for you two who have installed <place w:st="on">Vista, it will be backwards compatible for some years (disclaimer: I am led to believe).

The Download

The download contains VB.NET source code (easily ported via editor-macros to other languages I expect) that declares the WinAPI function SHGetFolderPathA and defines the constants required to use it. In addition several enumerations are given that help clarify virtual and physical folders, along with two wrapper methods; one that fetches the special folder path; the other that creates the special folder itself. There is no class library as it is probably not needed, but included is an executable that simply lists all the physical special folders it can find on your machine.

Download contents include:

File

Description and Content

modSpecialFolders.vb

Definition if CSIDL constants.
Declaration of shell32.dll function SHGetFolderPathA.
Public method CreateSpecialFolderPath.
Public method GetSpecialFolderPath.
Reasonably comprehensive in-line documentation of all the above.

enuCSIDL.vb

Definition of all CSIDL constants and flags as an enumeration.
Repetition of in-line documentation.

enuCSIDLFlags.vb

Redefinition of all CSIDL flag constants as an enumeration.
Repetition of in-line documentation.

enuCSIDLPhysical.vb

Redefinition of all CSIDL physical folder constants as an enumeration.
Repetition of in-line documentation.

enuCSIDLVirtual.vb

Redefinition of all CSIDL virtual folder constants as an enumeration.
Repetition of in-line documentation.

SpecialFolders.pdf

An early pdf version of this article.

Code Documentation

The following tables give abbreviated documentation for the dotNET methods and the WinAPI constants. All of this documentation is in the source code, in context.

dotNET SpecialDirectories Class shared methods:

dotNET function

Description

AllUsersApplicationData

Gets a path name pointing to the application's data in the
\Documents and Setting\All Users\ApplicationData directory.

CurrentUserApplicationData

Gets a path name pointing to the CurrentApplicationData directory.

Desktop

Gets a path name pointing to the Desktop directory.

MyDocuments

Gets a path name pointing to the MyDocuments directory.

MyMusic

Gets a path name pointing to the My Music directory.

MyPictures

Gets a path name pointing to the My Pictures directory.

ProgramFiles

Gets a path pointing to the Program Files directory.

Programs

Gets a path name pointing to the Programs directory.

Temp

Gets a path name pointing to the Temp directory.

WinAPI function SHGetFolderPathA CSIDL Constants (as defined by Author):

CSIDL Constant

Description

kCSIDL_ADMINTOOLS

The file system directory that is used to store administrative tools for an individual user.

kCSIDL_ALTSTARTUP

The file system directory that corresponds to the user's nonlocalized Startup program group.

kCSIDL_APPDATA

The file system directory that serves as a common repository for application-specific data.

kCSIDL_BITBUCKET

The virtual folder containing the objects in the user's Recycle Bin.

kCSIDL_CDBURN_AREA

The file system directory acting as a staging area for files waiting to be written to CD.

kCSIDL_COMMON_ADMINTOOLS

The file system directory containing administrative tools for all users of the computer.

kCSIDL_COMMON_ALTSTARTUP

The file system directory that corresponds to the nonlocalized Startup program group for all users.

kCSIDL_COMMON_APPDATA

The file system directory containing application data for all users.

kCSIDL_COMMON_DESKTOPDIRECTORY

The file system directory that contains files and folders that appear on the desktop for all users.

kCSIDL_COMMON_DOCUMENTS

The file system directory that contains documents that are common to all users.

kCSIDL_COMMON_FAVORITES

The file system directory that serves as a common repository for favorite items common to all users.

kCSIDL_COMMON_MUSIC

The file system directory that serves as a repository for music files common to all users.

kCSIDL_COMMON_OEM_LINKS

Links to All Users OEM specific apps.

kCSIDL_COMMON_PICTURES

The file system directory that serves as a repository for image files common to all users.

kCSIDL_COMMON_PROGRAMS

The file system directory that contains the directories for the common program groups that appear on the Start menu for all users.

kCSIDL_COMMON_STARTMENU

The file system directory that contains the programs and folders that appear on the Start menu for all users.

kCSIDL_COMMON_STARTUP

The file system directory that contains the programs that appear in the Startup folder for all users.

kCSIDL_COMMON_TEMPLATES

The file system directory that contains the templates that are available to all users.

kCSIDL_COMMON_VIDEO

The file system directory that serves as a repository for video files common to all users.

kCSIDL_COMPUTERSNEARME

Computers Near Me (computed from Workgroup membership). (virtual)

kCSIDL_CONNECTIONS

Network and Dial-up Connections.

kCSIDL_CONTROLS

The virtual folder containing icons for the Control Panel applications.

kCSIDL_COOKIES

The file system directory that serves as a common repository for Internet cookies.

kCSIDL_DESKTOP

The virtual folder representing the Windows desktop, the root of the namespace. Is actually a physical folder C:\Documents and Settings\Admin\Desktop.

kCSIDL_DESKTOPDIRECTORY

The file system directory used to physically store file objects on the desktop.

kCSIDL_DRIVES

The virtual folder representing My Computer, containing everything on the local computer: storage devices, printers, and Control Panel.

kCSIDL_FAVORITES

The file system directory that serves as a common repository for the user's favorite items.

kCSIDL_FONTS

A virtual folder containing fonts. A typical path is C:\Windows\Fonts. Is actually a physical folder.

kCSIDL_HISTORY

The file system directory that serves as a common repository for Internet history items.

kCSIDL_INTERNET

A virtual folder representing the Internet.

kCSIDL_INTERNET_CACHE

The file system directory that serves as a common repository for temporary Internet files.

kCSIDL_LOCAL_APPDATA

The file system directory that serves as a data repository for local (nonroaming) applications.

kCSIDL_MYDOCUMENTS

The virtual folder representing the My Documents desktop item.

kCSIDL_MYMUSIC

The file system directory that serves as a common repository for music files.

kCSIDL_MYPICTURES

The file system directory that serves as a common repository for image files.

kCSIDL_MYVIDEO

The file system directory that serves as a common repository for video files.

kCSIDL_NETHOOD

A file system directory containing the link objects that may exist in the My Network Places virtual folder.

kCSIDL_NETWORK

A virtual folder representing Network Neighborhood, the root of the network namespace hierarchy.

kCSIDL_PERSONAL

The virtual folder representing the My Documents desktop item. Is actually a physical folder C:\Documents and Settings\Admin\My Documents.

kCSIDL_PRINTERS

The virtual folder containing installed printers.

kCSIDL_PRINTHOOD

The file system directory that contains the link objects that can exist in the Printers virtual folder.

kCSIDL_PROFILE

The user's profile folder.

kCSIDL_PROFILES

The file system directory containing user profile folders.

kCSIDL_PROGRAM_FILES

The Program Files folder. A typical path is C:\Program Files.

kCSIDL_PROGRAM_FILES_COMMON

A folder for components that are shared across applications.

kCSIDL_PROGRAM_FILES_COMMONX86

x86 Program Files\Common on RISC.

kCSIDL_PROGRAM_FILESX86

x86 system directory on RISC.

kCSIDL_PROGRAM_FILESX86

x86 C:\Program Files on RISC.

kCSIDL_PROGRAMS

The file system directory that contains the user's program groups.

kCSIDL_RECENT

The file system directory that contains shortcuts to the user's most recently used documents.

kCSIDL_RESOURCES

%windir%\Resources\, For theme and other windows resources.

kCSIDL_RESOURCES_LOCALIZED

%windir%\Resources\[LangID], for theme and other windows specific resources.

kCSIDL_SENDTO

The file system directory that contains Send To menu items.

kCSIDL_STARTMENU

The file system directory containing Start menu items.

kCSIDL_STARTUP

The file system directory that corresponds to the user's Startup program group.

kCSIDL_SYSTEM

The Windows System folder.

kCSIDL_TEMPLATES

The file system directory that serves as a common repository for document templates.

kCSIDL_WINDOWS

The Windows directory or SYSROOT.

WinAPI function SHGetFolderPathA CSIDL Flags (defined by Author as KnownFolder flags for <place w:st="on">Vista):

CSIDL/KNOWNFOLDER Flags

Description

kKF_FLAG_SIMPLE_IDLIST

Build a simple pointer to an item identifier list (PIDL). (<place w:st="on">Vista)

kKF_FLAG_NOT_PARENT_RELATIVE

Gets the folder's default path independent of the current location of its parent. (<place w:st="on">Vista)

kKF_FLAG_DEFAULT_PATH

Gets the default path for a known folder that is redirected elsewhere.

kKF_FLAG_INIT

Initializes the folder using its Desktop.ini settings.

kKF_FLAG_NO_ALIAS

Gets the true system path for the folder, free of any aliased placeholders such as %USERPROFILE%.

kKF_FLAG_DONT_UNEXPAND

Stores the full path in the registry without environment strings. (<place w:st="on">Vista)

kKF_FLAG_DONT_VERIFY

Specifies not to verify the folder's existence before attempting to retrieve the path or IDList.

kKF_FLAG_CREATE

Forces the creation of the specified folder if that folder does not already exist.

Notes

The <place w:st="on">Vista equivalent of the WinAPI function SHGetFolderPath is SHGetKnownFolderPath.
The XP: SHGetFolderPath documentation says:

“Deprecated. Gets the path of a folder identified by a CSIDL value.
Note As of Windows Vista, this function is merely a wrapper for SHGetKnownFolderPath. The CSIDL value is translated to its associated KNOWNFOLDERID and then SHGetKnownFolderPath is called. New applications should use the known folder system rather than the older CSIDL system, which is supported only for backward compatibility.”

The <place w:st="on">Vista equivalents of the CSIDL constants are KNOWNFOLDERID constants (see references).

History

13-Mar-2008 [Warrick Procter] Article created.

References

dotNET: MSDN Special Directories

http://msdn2.microsoft.com/en-us/library/microsoft.visualbasic.fileio.specialdirectories_members(VS.80).aspx

VB: How to Use the SHGetFolderPath Function from Visual Basic

http://support.microsoft.com/kb/252652

XP: SHGetFolderPath Function

http://msdn2.microsoft.com/en-us/library/bb762181(VS.85).aspx

XP: CSIDL Constants

http://msdn2.microsoft.com/en-us/library/bb762494(VS.85).aspx

<place w:st="on">Vista: SHGetKnownFolderPath Function

http://msdn2.microsoft.com/en-us/library/bb762188(VS.85).aspx

<place w:st="on">Vista: KNOWNFOLDERID Constants

http://msdn2.microsoft.com/en-us/library/bb762584(VS.85).aspx

Appendix

Special Folder list output

SF# Special Folder Name Path
---- ------------------------ ---------------------------------------------------------------

000) Desktop C:\Documents and Settings\Admin\Desktop

001) Internet ==> Virtual Folder, does not exist.

002) Programs C:\Documents and Settings\Admin\Start Menu\Programs

003) Controls ==> Virtual Folder, does not exist.

004) Printers ==> Virtual Folder, does not exist.

005) Personal C:\Documents and Settings\Admin\My Documents

006) Favorites C:\Documents and Settings\Admin\Favorites

007) Startup C:\Documents and Settings\Admin\Start Menu\Programs\Startup

008) Recent C:\Documents and Settings\Admin\Recent

009) SendTo C:\Documents and Settings\Admin\SendTo

010) BitBucket ==> Virtual Folder, does not exist.

011) StartMenu C:\Documents and Settings\Admin\Start Menu

012) MyDocuments ==> Physical Folder, not created.

013) MyMusic C:\Documents and Settings\Admin\My Documents\My Music

014) MyVideo C:\Documents and Settings\Admin\My Documents\My Videos

016) DesktopDirectory C:\Documents and Settings\Admin\Desktop

017) Drives ==> Virtual Folder, does not exist.

018) Network ==> Virtual Folder, does not exist.

019) Nethood C:\Documents and Settings\Admin\NetHood

020) Fonts C:\WINDOWS\Fonts

021) Templates C:\Documents and Settings\Admin\Templates

022) CommonStartMenu C:\Documents and Settings\All Users\Start Menu

023) CommonPrograms C:\Documents and Settings\All Users\Start Menu\Programs

024) CommonStartup C:\Documents and Settings\All Users\Start Menu\Programs\Startup

025) CommonDesktopDirectory C:\Documents and Settings\All Users\Desktop

026) AppData C:\Documents and Settings\Admin\Application Data

027) Printhood C:\Documents and Settings\Admin\PrintHood

028) LocalAppdata C:\Documents and Settings\Admin\Local Settings\Application Data

029) AltStartup ==> Physical Folder, not created.

030) CommonAltStartup ==> Physical Folder, not created.

031) CommonFavorites C:\Documents and Settings\All Users\Favorites

032) InternetCache C:\Documents and Settings\Admin\Local Settings
\Temporary Internet Files
033) Cookies C:\Documents and Settings\Admin\Cookies

034) History C:\Documents and Settings\Admin\Local Settings\History

035) CommonAppData C:\Documents and Settings\All Users\Application Data

036) Windows C:\WINDOWS

037) System C:\WINDOWS\system32

038) ProgramFiles C:\Program Files

039) MyPictures C:\Documents and Settings\Admin\My Documents\My Pictures

040) Profile C:\Documents and Settings\Admin

041) SystemX86 C:\WINDOWS\system32

042) ProgramFilesX86 ==> Physical Folder, not created.

043) CommonProgramFiles C:\Program Files\Common Files

044) CommonProgramFilesX86 ==> Physical Folder, not created.

045) CommonTemplates C:\Documents and Settings\All Users\Templates

046) CommonDocuments C:\Documents and Settings\All Users\Documents

047) CommonAdminTools C:\Documents and Settings\All Users\Start Menu\Programs
\Administrative Tools
048) AdminTools C:\Documents and Settings\Admin\Start Menu\Programs
\Administrative Tools
049) Connections ==> Virtual Folder, does not exist.

053) CommonMusic C:\Documents and Settings\All Users\Documents\My Music

054) CommonPictures C:\Documents and Settings\All Users\Documents\My Pictures

055) CommonVideo C:\Documents and Settings\All Users\Documents\My Videos

056) Resources C:\WINDOWS\resources

057) ResourcesLocalized ==> Physical Folder, not created.

058) CommonOEMLinks ==> Virtual Folder, does not exist.

059) CDBurnArea C:\Documents and Settings\Admin\Local Settings\Application Data
\Microsoft\CD Burning
061) ComputersNearMe ==> Virtual Folder, does not exist.

062) Profiles ==> Physical Folder, not created.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)