Click here to Skip to main content
15,888,239 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 153.2K   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

 
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 
GeneralRe: Two alternative implementations Pin
Babs Iwarere20-Mar-07 9:25
Babs Iwarere20-Mar-07 9:25 
QuestionSo... now let's post article on single API call? Pin
Brian V Shifrin22-Feb-02 4:38
Brian V Shifrin22-Feb-02 4:38 
AnswerRe: So... now let's post article on single API call? Pin
Amanjit Gill22-Feb-02 16:27
Amanjit Gill22-Feb-02 16:27 
AnswerRe: So... now let's post article on single API call? Pin
perlmunger23-Feb-02 6:18
perlmunger23-Feb-02 6:18 
Let's assume for a minute that the person who posted this is a young programmer. He found something that was a revelation to him. He felt that posting it would be a help to someone else. Then he finds out that his revelation is not welcome because it's an "article on single API call". I'm sure he's very encuoraged to improve and try to post something else in the future (please notice the sarcasm).

On behalf of the software developers who actually believe that people have to start somewhere and that our first priority should be to help others learn, I apologize to the poster of this article. Please don't let a few insensitive people discourage you. Keep up the good work!

-Matt
GeneralRe: So... now let's post article on single API call? Pin
Mike Klimentiev25-Feb-02 8:41
Mike Klimentiev25-Feb-02 8:41 
GeneralRe: So... now let's post article on single API call? Pin
User 842025-Feb-02 16:31
User 842025-Feb-02 16:31 
GeneralRe: So... now let's post article on single API call? Pin
Nish Nishant25-Feb-02 17:24
sitebuilderNish Nishant25-Feb-02 17:24 
AnswerRe: So... now let's post article on single API call? Pin
Anonymous4-Nov-02 5:07
Anonymous4-Nov-02 5:07 

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.