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:
- The current working, and executable file directories.
- The Program Files, Windows, and system directories.
- The My Documents, My Music, My Pictures, and My Videos directories.
- The application data and local application data directories.
- 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
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:
- 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).
- Add the line
#include “Util.h”
in the top section of the *.cpp files you intend to use these functions in. - Simply call the required function, e.g.,
stlString strMyDocuments = CUtil::GetMyDocumentsDirectory()
. - The
stlString
"strMyDocuments
" will now have the file path to the directory specified. - Take a look at all the button event handler functions in the source code to see steps 1 to 4 in action.
- 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