Click here to Skip to main content
Licence CPOL
First Posted 21 Feb 2002
Views 117,931
Bookmarked 24 times

A function to retrieve the directory of a program

By | 24 Feb 2002 | Article
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)

About the Author

Brad Bruce

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionRtnVal's 'lifetime' PinmemberLino Masoni22:09 10 Nov '09  
QuestionHow is this more reliable than _getcwd() Pinmemberdrake2814:13 28 Dec '04  
AnswerRe: How is this more reliable than _getcwd() PinmemberBrad Bruce15:05 28 Dec '04  
GeneralRe: How is this more reliable than _getcwd() Pinmemberdrake2813:16 29 Dec '04  
Generalthanks for the effort!! Pinmemberf214:36 4 Oct '04  
GeneralWon't this do... PinsussAnonymous5:03 15 Apr '04  
GeneralRe: Won't this do... PinmemberBrad Bruce1:36 30 Apr '04  
GeneralThe way i've been doing it till now Pinmembergopya6:25 12 May '02  
Generala small question about GetModuleFileName PinmemberAlex20020:03 26 Feb '02  
GeneralRe: a small question about GetModuleFileName PinmemberAnonymous21:52 26 Feb '02  
GeneralRe: a small question about GetModuleFileName PinsubeditorNishant S15:24 15 Jul '02  
GeneralUpdates PinmemberBrad Bruce13:14 25 Feb '02  
GeneralRe: Updates PinmemberNish [BusterBoy]17:28 25 Feb '02  
GeneralRe: Updates PinmemberBrad Bruce0:14 26 Feb '02  
GeneralGreat work if... PinmemberCMFC6.0User4:52 25 Feb '02  
GeneralRe: Great work if... PinmemberBrad Bruce13:03 25 Feb '02  
GeneralRe: Great work if... PinmemberTim Smith15:09 25 Feb '02  
GeneralRe: Great work if... PinmemberCMFC6.0User6:10 26 Feb '02  
GeneralOFN_NOCHANGEDIR PinmemberMat Kramer4:29 25 Feb '02  
GeneralSimpler solution PinmemberAnonymous3:48 24 Feb '02  
GeneralRe: Simpler solution PinmemberTim Smith15:11 25 Feb '02  
GeneralRe: Simpler solution PinmemberMike Klimentiev8:11 26 Feb '02  
GeneralRe: Simpler solution PinmemberTim Smith8:23 26 Feb '02  
GeneralThanks for the article... PinmemberMatt Gullett7:16 22 Feb '02  
GeneralTwo alternative implementations PinmemberCarl Berg5:01 22 Feb '02  

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.

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