A function to retrieve the directory of a program

转载 2006年05月19日 13:42:00

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.

http://www.codeproject.com/tips/programdir.asp?df=100&forumid=3336&exp=0&select=121652

相关文章推荐

The quiz 1 of Intro to Parallel Program

Homework 1 Color to Greyscale Conversion   A common way to represent color images isknown as RGBA - ...

7 Strace Examples to Debug the Execution of a Program in Linux

Strace is a debugging tool that will help you troubleshoot issues. Strace monitors the system cal...

Code to print the byte representation of program objects

This code uses casting to circumvent the type system. Firstly, cast pointers to byte_pointer, pointi...

Portfolio of Mouwu Lin for application to the Emerging Media Program in CMU

This is the portfolio of Mouwu Lin for the application to the Emerging Media Program in CMU.

When many files was in deep directory of eMMC, Nozomi crash when connected to PC

78% INFO 02-18 13:02:28.583 (135:135) DEBUG Opening /data/log/debuggerd.log 78% INFO 02-18 13:02:28...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:A function to retrieve the directory of a program
举报原因:
原因补充:

(最多只允许输入30个字)