Click here to Skip to main content
13,454,124 members (55,597 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 21 Feb 2002

A function to retrieve the directory of a program

, 24 Feb 2002
Rate this:
Please Sign up or sign in to vote.
A small function to retrieve the directory a program is being executed from


I've needed to get the directory a program was running several times. I've used it to determine where configuration files could be found. I've used it for determining where images should be stored/loaded. I've also had to write it from scratch several times :( . I'm submitting it here so hopefully someone else doesn't have to figure it out themselves.

I've found that using _getcwd isn't accurate whenever the user is allowed to use the file dialogs to open or save files. If they move to a different directory in the file dialogs, the current working directory is changed as well

CString GetProgramDir()
    CString RtnVal;
    char    FileName[MAX_PATH];
    GetModuleFileName(AfxGetInstanceHandle(), FileName, MAX_PATH);
    RtnVal = FileName;
    RtnVal = RtnVal.Left(RtnVal.ReverseFind('\\'));
    return RtnVal;

It's not much, but I've found it helpful and hope that someone else will as well.


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


About the Author

Brad Bruce
Architect TRAC Intermodal
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralWon't this do... Pin
Anonymous15-Apr-04 5:03
sussAnonymous15-Apr-04 5:03 
GeneralRe: Won't this do... Pin
Brad Bruce30-Apr-04 1:36
memberBrad Bruce30-Apr-04 1:36 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180321.1 | Last Updated 25 Feb 2002
Article Copyright 2002 by Brad Bruce
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid