Working Directory and File issues

I'm running into working directory issues where I need to open a file from the base directory/templates. If you click on the shortcut the installation creates, the file can't be found. I figure this is because the program is trying to open /teamplates/file and the working directory is who knows where! My solution is to read from the registry to get the installation base directory and go from there. This seems kind of stupid to have to read the registry whenever I need to access a file. Is there any easier way. I suppose I could read the variable once when my View gets created, then pass it to all its members who need it... And what about testing? Am I going to create a fake entry for now until I get towards final testing where installwizard creates its own entries? Comments anyone?!

Justin Deltener
deltener@mindtremors.com
DredwickAsked:
Who is Participating?
 
chensuConnect With a Mentor Commented:
Yes, you can read the registry key once your application starts. You don't have to do it every time you access a file. Another way is to consider the full path name of your application as the base directory. For example,

TCHAR pszBuf[_MAX_PATH];
::GetModuleFileName(NULL, pszBuf, sizeof(pszBuf) / sizeof(TCHAR));
*::_tcsrchr(pszBuf, _T('\\')) = _T('\0');
::lstrcat(pszBuf, _T("\\teamplates\\thefile.ext"));
0
 
DredwickAuthor Commented:
I've never even heard of the GetModuleFileName function. Perfect solution! Exactly what I needed, Great Job!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.