Use SHGetFolderPath to find popular shell folders |
Are you sure that document you're writing out belongs on the local drive in "C:\My Documents"? Popular shell folders such as "My Documents" are logical folders that may or may not point to the default storage location with the same directory name. Not only can a user of a stand alone machine easily change the folder something like "My Documents" points to on their own, but in the managed environment document storage usually lives on network shares, and those shares may change at any time. So not only is it a bad idea to hardcode full paths like "C:\My Documents" into your application, you need to be checking the path each and every time you open shell folders.
By using the Win32 API SHGetFolderPath to retrieve the path to special shell folders each and every time, your application will be able to dynamically cope with its changing environment. Using SHGetFolderPath in conjunction with the new CSIDLs (logical directory indicators), you can easily retrieve the correct location for various types of data. CSIDLs provide a unique system-independent way to identify these special folders. There are CSIDLs for almost every important shell folder. CSIDL_PERSONAL, CSIDL_COMMON_DOCUMENTS, CSIDL_FAVORITES and CSIDL_TEMPLATES are just a few of the ones more commonly used. See the platform SDK for a list of CSIDLs for the common locations.
While it is possible to obtain folder paths by other means, remember that an administrator can change the path pointed to by these logical folders at any time. This means that unless you query for the logical folder using a call to SHGetFolderPath you may have out of date path information.
System support for SHGetFolderPath is not native on Windows 98, Windows 95, and Windows NT 4.0. These downlevel clients will require the installation of new system files prior to use. (Redistribution specifics not available at press time)