Click here to Skip to main content
15,886,258 members
Articles / Mobile Apps / Windows Mobile

Get useful directory paths

Rate me:
Please Sign up or sign in to vote.
4.00/5 (12 votes)
18 Jan 2009CPOL2 min read 40.3K   671   19   12
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)


Written By
Chief Technology Officer Mind & Machines
Bangladesh Bangladesh
(AKA The Freak), besides being a computer nerd, given his love for extreme sports, is a fire spinner, sky diver, and a XC/DH biker. He juggles his time between computing, research, business, travelling, gourmet cooking, and many other bizarre hobbies.

Comments and Discussions

 
GeneralThank you !! Pin
Rany39-Mar-09 12:30
Rany39-Mar-09 12:30 
GeneralRe: Thank you !! Pin
Shup9-Mar-09 12:40
Shup9-Mar-09 12:40 
GeneralRe: Thank you !! [modified] Pin
Rany310-Mar-09 2:37
Rany310-Mar-09 2:37 
GeneralRe: Thank you !! Pin
Shup10-Mar-09 2:57
Shup10-Mar-09 2:57 
GeneralRe: Thank you !! Pin
Rany310-Mar-09 3:39
Rany310-Mar-09 3:39 
GeneralRe: Thank you !! Pin
Shup10-Mar-09 4:08
Shup10-Mar-09 4:08 
GeneralRe: Thank you !! [modified] Pin
Rany310-Mar-09 4:28
Rany310-Mar-09 4:28 
GeneralRe: Thank you !! Pin
Shup10-Mar-09 13:43
Shup10-Mar-09 13:43 
GeneralThanks! Pin
UGoetzke19-Jan-09 6:14
UGoetzke19-Jan-09 6:14 
GeneralRe: Thanks! Pin
Shup19-Jan-09 6:40
Shup19-Jan-09 6:40 
GeneralMy vote of 1 Pin
KarstenK18-Jan-09 20:57
mveKarstenK18-Jan-09 20:57 
GeneralRe: My vote of 1 Pin
Shup19-Jan-09 1:04
Shup19-Jan-09 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.