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

Get useful directory paths

4.00/5 (12 votes)
18 Jan 2009CPOL2 min read 41.2K   673  
An article on easily finding the paths of commonly used user specific directories.

Image 1

Introduction

I’ve come across countless situations where routinely used utilitarian functions are simply not available, and coders simply have to write and rewrite them many times over. This is one of many articles in a series which will try to provide a collection of routinely used functions with example applications on how to use them, in the hope that they will be as useful to the many developers out there as it has been to me.

The functions highlighted in this article simply get:

  1. The current working, and executable file directories.
  2. The Program Files, Windows, and system directories.
  3. The My Documents, My Music, My Pictures, and My Videos directories.
  4. The application data and local application data directories.
  5. The desktop and startup directories.

I’ve found these simple functions to be extremely useful in many projects, which required reading from or saving to those directories, some of which are user specific.

Available Functions

C++
//////////////////////////////////////////////////
// Get commonly used directory paths
//////////////////////////////////////////////////
static stlString GetWorkingDirectory();
static stlString GetProgramDirectory();

static stlString GetProgramFilesDirectory();
static stlString GetWindowsDirectory();
static stlString GetSystemDirectory();

static stlString GetMyDocumentsDirectory();
static stlString GetMyMusicDirectory();
static stlString GetMyPicturesDirectory();
static stlString GetMyVideosDirectory();

static stlString GetAppDataDirectory();
static stlString GetLocalAppDataDirectory();

static stlString GetDesktopDirectory();
static stlString GetStartupDirectory();
//////////////////////////////////////////////////

Background

These functions were developed a few years ago when a couple of projects required reading and writing user specific data to these commonly used folders.

Using the Code

Follow these simple steps to use the code in your project:

  1. Add the files Util.h and Util.cpp into your Visual Studio C++ project (or any other kind of project; I have never used the code on anything other than Visual Studio C++ projects, so any input on using it on other C++ projects is appreciated).
  2. Add the line #include “Util.h” in the top section of the *.cpp files you intend to use these functions in.
  3. Simply call the required function, e.g., stlString strMyDocuments = CUtil::GetMyDocumentsDirectory().
  4. The stlString "strMyDocuments" will now have the file path to the directory specified.
  5. Take a look at all the button event handler functions in the source code to see steps 1 to 4 in action.
  6. Check the "Util.cpp" source file to understand how the function is working internally, it is quite self explanatory.

Points of Interest

The code provided here is for unmanaged C++. If you do find the code useful, please leave a comment, it could make my day :-)

History

  • V1.0 - Initial release.

License

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