Wednesday, December 24, 2008

[VC++] Getting special folder path in your program

Special Folders are folders which are presented to the user through an interface as an abstract concept, instead of an absolute folder path. This makes it possible for an application to ask the operating system where an appropriate location for certain kinds of files can be found, regardless of what version or language of operating system is being used.
Some example of special Folders are,My Documents, Desktop Directory, History etc

Inorder to obtain the special folders programmatically use the shell function SHGetSpecialFolderPath(). Below code snippet shows how to obtain the Special folder path of your desktop.

// Allocating string that receives the drive and path
char* szPath = new char[MAX_PATH];
// Getting folder path of Desktop
if( TRUE == SHGetSpecialFolderPath( 0, szPath, CSIDL_DESKTOP, FALSE ))
{
// Successfully retrieved the path in szPath, now you can use
MessageBox( szPath );
}
delete []szPath;

Here[^] is a list of constant special item ID list for getting other special folders, as we used CSIDL_DESKTOP for getting Desktop folder path.


No comments: