# A function to retrieve the directory of a program

500人阅读 评论(0)

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

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：10253次
• 积分：161
• 等级：
• 排名：千里之外
• 原创：5篇
• 转载：7篇
• 译文：0篇
• 评论：4条
文章分类
最新评论