Click here to Skip to main content
11,705,453 members (50,828 online)
Click here to Skip to main content

A function to retrieve the directory of a program

, 24 Feb 2002 CPOL 128.3K 25
Rate this:
Please Sign up or sign in to vote.
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 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.

License

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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionRtnVal's 'lifetime' Pin
Lino Masoni10-Nov-09 22:09
memberLino Masoni10-Nov-09 22:09 
QuestionHow is this more reliable than _getcwd() Pin
drake2828-Dec-04 14:13
memberdrake2828-Dec-04 14:13 
So what is some smart-ass user moved your exec to another folder, and the .ini files are still in the original folder?? There are many ways to screw up a function if someone really wants to...

Why not just call _getcwd() in OnInitialUpdate() and store the app-path in the registry... BEFORE any dialogs are opened. Presumably, the app was installed with your installer, and is being run from your shortcuts...

Sure, someone could manually create a shortcut and mess with the "Start in Folder" setting to "trick" your app... but realistically?? If so, I have no problem showing them an alert.

AnswerRe: How is this more reliable than _getcwd() Pin
Brad Bruce28-Dec-04 15:05
memberBrad Bruce28-Dec-04 15:05 
GeneralRe: How is this more reliable than _getcwd() Pin
drake2829-Dec-04 13:16
memberdrake2829-Dec-04 13:16 
Generalthanks for the effort!! Pin
f24-Oct-04 14:36
memberf24-Oct-04 14:36 
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 
GeneralThe way i've been doing it till now Pin
gopya12-May-02 6:25
membergopya12-May-02 6:25 
Generala small question about GetModuleFileName Pin
Alex200226-Feb-02 0:03
memberAlex200226-Feb-02 0:03 
GeneralRe: a small question about GetModuleFileName Pin
Anonymous26-Feb-02 21:52
memberAnonymous26-Feb-02 21:52 
GeneralRe: a small question about GetModuleFileName Pin
Nishant S15-Jul-02 15:24
subeditorNishant S15-Jul-02 15:24 
GeneralUpdates Pin
Brad Bruce25-Feb-02 13:14
memberBrad Bruce25-Feb-02 13:14 
GeneralRe: Updates Pin
Nish [BusterBoy]25-Feb-02 17:28
memberNish [BusterBoy]25-Feb-02 17:28 
GeneralRe: Updates Pin
Brad Bruce26-Feb-02 0:14
memberBrad Bruce26-Feb-02 0:14 
GeneralGreat work if... Pin
CMFC6.0User25-Feb-02 4:52
memberCMFC6.0User25-Feb-02 4:52 
GeneralRe: Great work if... Pin
Brad Bruce25-Feb-02 13:03
memberBrad Bruce25-Feb-02 13:03 
GeneralRe: Great work if... Pin
Tim Smith25-Feb-02 15:09
memberTim Smith25-Feb-02 15:09 
GeneralRe: Great work if... Pin
CMFC6.0User26-Feb-02 6:10
memberCMFC6.0User26-Feb-02 6:10 
GeneralOFN_NOCHANGEDIR Pin
Mat Kramer25-Feb-02 4:29
memberMat Kramer25-Feb-02 4:29 
GeneralSimpler solution Pin
Anonymous24-Feb-02 3:48
memberAnonymous24-Feb-02 3:48 
GeneralRe: Simpler solution Pin
Tim Smith25-Feb-02 15:11
memberTim Smith25-Feb-02 15:11 
GeneralRe: Simpler solution Pin
Mike Klimentiev26-Feb-02 8:11
memberMike Klimentiev26-Feb-02 8:11 
GeneralRe: Simpler solution Pin
Tim Smith26-Feb-02 8:23
memberTim Smith26-Feb-02 8:23 
GeneralThanks for the article... Pin
Matt Gullett22-Feb-02 7:16
memberMatt Gullett22-Feb-02 7:16 
GeneralTwo alternative implementations Pin
Carl Berg22-Feb-02 5:01
memberCarl Berg22-Feb-02 5:01 
GeneralRe: Two alternative implementations Pin
Babs Iwarere20-Mar-07 9:25
memberBabs Iwarere20-Mar-07 9:25 
QuestionSo... now let's post article on single API call? Pin
Brian V Shifrin22-Feb-02 4:38
memberBrian V Shifrin22-Feb-02 4:38 
AnswerRe: So... now let's post article on single API call? Pin
Amanjit Gill22-Feb-02 16:27
memberAmanjit Gill22-Feb-02 16:27 
AnswerRe: So... now let's post article on single API call? Pin
perlmunger23-Feb-02 6:18
memberperlmunger23-Feb-02 6:18 
GeneralRe: So... now let's post article on single API call? Pin
Mike Klimentiev25-Feb-02 8:41
memberMike Klimentiev25-Feb-02 8:41 
GeneralRe: So... now let's post article on single API call? Pin
Mike Mullikin25-Feb-02 16:31
memberMike Mullikin25-Feb-02 16:31 
GeneralRe: So... now let's post article on single API call? Pin
Nish [BusterBoy]25-Feb-02 17:24
memberNish [BusterBoy]25-Feb-02 17:24 
AnswerRe: So... now let's post article on single API call? Pin
Anonymous4-Nov-02 5:07
sussAnonymous4-Nov-02 5:07 

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
Web03 | 2.8.150819.1 | Last Updated 25 Feb 2002
Article Copyright 2002 by Brad Bruce
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid