Click here to Skip to main content
11,412,182 members (65,103 online)
Click here to Skip to main content

A function to retrieve the directory of a program

, 24 Feb 2002 CPOL
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 Frown | :( . 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

Comments and Discussions

QuestionRtnVal's 'lifetime' PinmemberLino Masoni10-Nov-09 23:09 
QuestionHow is this more reliable than _getcwd() Pinmemberdrake2828-Dec-04 15:13 
AnswerRe: How is this more reliable than _getcwd() PinmemberBrad Bruce28-Dec-04 16:05 
GeneralRe: How is this more reliable than _getcwd() Pinmemberdrake2829-Dec-04 14:16 
Generalthanks for the effort!! Pinmemberf24-Oct-04 15:36 
GeneralWon't this do... PinsussAnonymous15-Apr-04 6:03 
GeneralRe: Won't this do... PinmemberBrad Bruce30-Apr-04 2:36 
GeneralThe way i've been doing it till now Pinmembergopya12-May-02 7:25 
This is how i do it ....

CString GetAppPath( void )
// get the app path from the help path
CString sAppPath = AfxGetApp()->m_pszHelpFilePath;
int iPos = sAppPath.ReverseFind( '\\' );
sAppPath = sAppPath.Mid( 0, iPos );
return sAppPath;

i was tired of finding a way to get the exe path, as i needed to access config files from there, and i had not read this post here, so i hacked up this little trick, works fine for me ...
of course this will only work if the help file path has not been changed, which i dont Smile | :) .

Gopal Raichur
Generala small question about GetModuleFileName PinmemberAlex200226-Feb-02 1:03 
GeneralRe: a small question about GetModuleFileName PinmemberAnonymous26-Feb-02 22:52 
GeneralRe: a small question about GetModuleFileName PinsubeditorNishant S15-Jul-02 16:24 
GeneralUpdates PinmemberBrad Bruce25-Feb-02 14:14 
GeneralRe: Updates PinmemberNish [BusterBoy]25-Feb-02 18:28 
GeneralRe: Updates PinmemberBrad Bruce26-Feb-02 1:14 
GeneralGreat work if... PinmemberCMFC6.0User25-Feb-02 5:52 
GeneralRe: Great work if... PinmemberBrad Bruce25-Feb-02 14:03 
GeneralRe: Great work if... PinmemberTim Smith25-Feb-02 16:09 
GeneralRe: Great work if... PinmemberCMFC6.0User26-Feb-02 7:10 
GeneralOFN_NOCHANGEDIR PinmemberMat Kramer25-Feb-02 5:29 
GeneralSimpler solution PinmemberAnonymous24-Feb-02 4:48 
GeneralRe: Simpler solution PinmemberTim Smith25-Feb-02 16:11 
GeneralRe: Simpler solution PinmemberMike Klimentiev26-Feb-02 9:11 
GeneralRe: Simpler solution PinmemberTim Smith26-Feb-02 9:23 
GeneralThanks for the article... PinmemberMatt Gullett22-Feb-02 8:16 
GeneralTwo alternative implementations PinmemberCarl Berg22-Feb-02 6:01 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 25 Feb 2002
Article Copyright 2002 by Brad Bruce
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid