Click here to Skip to main content
Licence CPOL
First Posted 18 Jan 2009
Views 14,863
Downloads 238
Bookmarked 17 times

Get useful directory paths

By | 18 Jan 2009 | Article
An article on easily finding the paths of commonly used user specific directories.

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

//////////////////////////////////////////////////
// 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)

About the Author

Shup

Chief Technology Officer
Mind & Machines
Bangladesh Bangladesh

Member

(AKA The Freak), besides being a computer geek, given his love for extreme sports, is a fire spinner, sky diver, and a XC/DH biker. He juggles his time between being a business man, computer scientist, a researcher in robotics, and many other bizarre hobbies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank you !! PinmemberRany312:30 9 Mar '09  
GeneralRe: Thank you !! PinmemberShup12:40 9 Mar '09  
GeneralRe: Thank you !! [modified] PinmemberRany32:37 10 Mar '09  
GeneralRe: Thank you !! PinmemberShup2:57 10 Mar '09  
GeneralRe: Thank you !! PinmemberRany33:39 10 Mar '09  
GeneralRe: Thank you !! PinmemberShup4:08 10 Mar '09  
GeneralRe: Thank you !! [modified] PinmemberRany34:28 10 Mar '09  
GeneralRe: Thank you !! PinmemberShup13:43 10 Mar '09  
GeneralThanks! PinmemberUGoetzke6:14 19 Jan '09  
GeneralRe: Thanks! PinmemberShup6:40 19 Jan '09  
GeneralMy vote of 1 PinmemberKarstenK20:57 18 Jan '09  
GeneralRe: My vote of 1 PinmemberShup1:04 19 Jan '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 18 Jan 2009
Article Copyright 2009 by Shup
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid