Click here to Skip to main content
Click here to Skip to main content

A function to retrieve the directory of a program

By , 24 Feb 2002
 

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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionRtnVal's 'lifetime'memberLino Masoni10 Nov '09 - 22:09 
QuestionHow is this more reliable than _getcwd()memberdrake2828 Dec '04 - 14:13 
AnswerRe: How is this more reliable than _getcwd()memberBrad Bruce28 Dec '04 - 15:05 
GeneralRe: How is this more reliable than _getcwd()memberdrake2829 Dec '04 - 13:16 
Generalthanks for the effort!!memberf24 Oct '04 - 14:36 
GeneralWon't this do...sussAnonymous15 Apr '04 - 5:03 
GeneralRe: Won't this do...memberBrad Bruce30 Apr '04 - 1:36 
GeneralThe way i've been doing it till nowmembergopya12 May '02 - 6:25 
Generala small question about GetModuleFileNamememberAlex200226 Feb '02 - 0:03 
GeneralRe: a small question about GetModuleFileNamememberAnonymous26 Feb '02 - 21:52 
GeneralRe: a small question about GetModuleFileNamesubeditorNishant S15 Jul '02 - 15:24 
GeneralUpdatesmemberBrad Bruce25 Feb '02 - 13:14 
GeneralRe: UpdatesmemberNish [BusterBoy]25 Feb '02 - 17:28 
GeneralRe: UpdatesmemberBrad Bruce26 Feb '02 - 0:14 
GeneralGreat work if...memberCMFC6.0User25 Feb '02 - 4:52 
GeneralRe: Great work if...memberBrad Bruce25 Feb '02 - 13:03 
GeneralRe: Great work if...memberTim Smith25 Feb '02 - 15:09 
GeneralRe: Great work if...memberCMFC6.0User26 Feb '02 - 6:10 
GeneralOFN_NOCHANGEDIRmemberMat Kramer25 Feb '02 - 4:29 
GeneralSimpler solutionmemberAnonymous24 Feb '02 - 3:48 
GeneralRe: Simpler solutionmemberTim Smith25 Feb '02 - 15:11 
GeneralRe: Simpler solutionmemberMike Klimentiev26 Feb '02 - 8:11 
GeneralRe: Simpler solutionmemberTim Smith26 Feb '02 - 8:23 
GeneralThanks for the article...memberMatt Gullett22 Feb '02 - 7:16 
GeneralTwo alternative implementationsmemberCarl Berg22 Feb '02 - 5:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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