Click here to Skip to main content
Click here to Skip to main content
Go to top

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 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
Web Developer
United States United States
No Biography provided

Comments and Discussions

QuestionRtnVal's 'lifetime' PinmemberLino Masoni10-Nov-09 22:09 
QuestionHow is this more reliable than _getcwd() Pinmemberdrake2828-Dec-04 14:13 
AnswerRe: How is this more reliable than _getcwd() PinmemberBrad Bruce28-Dec-04 15:05 
I've had WAY too many people creating desktop shortcuts and leave off the "Start in Folder" field altogether.
If we find users moving the program, we let our tech-support team loose on them. (Those are NOT kind and gentle phone calls!!!)
Many of our customers don't allow their users have access to the registry either.
There are many methods for doing this, as well as many ways to corrupt it, but it's been working for us for about 7 years now!
Thanks for the comment though. It's always good to get another opinion.
GeneralRe: How is this more reliable than _getcwd() Pinmemberdrake2829-Dec-04 13:16 
Generalthanks for the effort!! Pinmemberf24-Oct-04 14:36 
GeneralWon't this do... PinsussAnonymous15-Apr-04 5:03 
GeneralRe: Won't this do... PinmemberBrad Bruce30-Apr-04 1:36 
GeneralThe way i've been doing it till now Pinmembergopya12-May-02 6:25 
Generala small question about GetModuleFileName PinmemberAlex200226-Feb-02 0:03 
GeneralRe: a small question about GetModuleFileName PinmemberAnonymous26-Feb-02 21:52 

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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 25 Feb 2002
Article Copyright 2002 by Brad Bruce
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid