Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

A function to retrieve the directory of a program

Rate me:
Please Sign up or sign in to vote.
3.15/5 (13 votes)
24 Feb 2002CPOL 152.7K   25   33
A small function to retrieve the directory a program is being executed from

Introduction

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.

License

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


Written By
Architect TRAC Intermodal
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRtnVal's 'lifetime' Pin
Lino Masoni10-Nov-09 22:09
Lino Masoni10-Nov-09 22:09 
QuestionHow is this more reliable than _getcwd() Pin
Douglas R. Keesler28-Dec-04 14:13
Douglas R. Keesler28-Dec-04 14:13 
AnswerRe: How is this more reliable than _getcwd() Pin
Brad Bruce28-Dec-04 15:05
Brad Bruce28-Dec-04 15:05 
GeneralRe: How is this more reliable than _getcwd() Pin
Douglas R. Keesler29-Dec-04 13:16
Douglas R. Keesler29-Dec-04 13:16 
Generalthanks for the effort!! Pin
f24-Oct-04 14:36
f24-Oct-04 14:36 
...

from,
-= aLbert =-

GeneralWon't this do... Pin
Anonymous15-Apr-04 5:03
Anonymous15-Apr-04 5:03 
GeneralRe: Won't this do... Pin
Brad Bruce30-Apr-04 1:36
Brad Bruce30-Apr-04 1:36 
GeneralThe way i've been doing it till now Pin
gopya12-May-02 6:25
gopya12-May-02 6:25 
Generala small question about GetModuleFileName Pin
26-Feb-02 0:03
suss26-Feb-02 0:03 
GeneralRe: a small question about GetModuleFileName Pin
26-Feb-02 21:52
suss26-Feb-02 21:52 
GeneralRe: a small question about GetModuleFileName Pin
Nish Nishant15-Jul-02 15:24
sitebuilderNish Nishant15-Jul-02 15:24 
GeneralUpdates Pin
Brad Bruce25-Feb-02 13:14
Brad Bruce25-Feb-02 13:14 
GeneralRe: Updates Pin
Nish Nishant25-Feb-02 17:28
sitebuilderNish Nishant25-Feb-02 17:28 
GeneralRe: Updates Pin
Brad Bruce26-Feb-02 0:14
Brad Bruce26-Feb-02 0:14 
GeneralGreat work if... Pin
CMFC6.0VS.NETUser25-Feb-02 4:52
CMFC6.0VS.NETUser25-Feb-02 4:52 
GeneralRe: Great work if... Pin
Brad Bruce25-Feb-02 13:03
Brad Bruce25-Feb-02 13:03 
GeneralRe: Great work if... Pin
Tim Smith25-Feb-02 15:09
Tim Smith25-Feb-02 15:09 
GeneralRe: Great work if... Pin
CMFC6.0VS.NETUser26-Feb-02 6:10
CMFC6.0VS.NETUser26-Feb-02 6:10 
GeneralOFN_NOCHANGEDIR Pin
Mat Kramer25-Feb-02 4:29
Mat Kramer25-Feb-02 4:29 
GeneralSimpler solution Pin
24-Feb-02 3:48
suss24-Feb-02 3:48 
GeneralRe: Simpler solution Pin
Tim Smith25-Feb-02 15:11
Tim Smith25-Feb-02 15:11 
GeneralRe: Simpler solution Pin
Mike Klimentiev26-Feb-02 8:11
Mike Klimentiev26-Feb-02 8:11 
GeneralRe: Simpler solution Pin
Tim Smith26-Feb-02 8:23
Tim Smith26-Feb-02 8:23 
GeneralThanks for the article... Pin
Matt Gullett22-Feb-02 7:16
Matt Gullett22-Feb-02 7:16 
GeneralTwo alternative implementations Pin
C-J Berg22-Feb-02 5:01
C-J Berg22-Feb-02 5:01 

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.