Setting the directory for an SDI application

IssacJones
IssacJones used Ask the Experts™
on
Hi

How can I set the default directory (folder) that will be opened/displayed when trying to open a file in an SDI application?

John
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
'SetCurrentDirectory()' (http://msdn.microsoft.com/en-us/library/aa365530(VS.85).aspx) should help you here.

Commented:
SetCurrentDirectoy doesn't work here

you must override OpenDocumentFile()-Function and bring it public
void CMainFrame::OnFileOpen()
{
	CFileDialog fd(TRUE, 0, _T("D:\\"));
	if(fd.DoModal() == IDOK){
		theApp.OpenDocumentFile(fd.GetPathName());
	}
}

Open in new window

Commented:
SetCurrentDirectory works if you put in Initinstance, but only once, if the Folder once was changed, your default comes never back. the function OpenDocuemntFile is called after the dialog is closed
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

Commented:
I was thinking more about a setting in CWinApp i.e. a setting for the application. Can this not be done? I don't really want to override OnFileOpen.

John

Commented:
for default values?
you can only override the rare documented CDocumentManager class, it's part of CWinApp and manage the filehandling

Author

Commented:
yes, a way of overriding CDocumentManager class would be better.

John
Commented:
hiere is the source of the needed function, from anywhere of the libraries
i've overwritten some year ago
CWinApp call this function to open a file
CDocument* CDataProDocManager::OpenDocumentFile(LPCTSTR lpszFileName)
{
#ifdef DEBUG_DOCMANAGER
	TRACE("CDataProDocManager::OpenDocumentFile(%s)\n", lpszFileName);
#endif

	if (lpszFileName == NULL)
	{
		AfxThrowInvalidArgException();
	}
	// find the highest confidence
	POSITION pos = m_templateList.GetHeadPosition();
	CDocTemplate::Confidence bestMatch = CDocTemplate::noAttempt;
	CDocTemplate* pBestTemplate = NULL;
	CDocument* pOpenDocument = NULL;

	TCHAR szPath[_MAX_PATH];
	ASSERT(lstrlen(lpszFileName) < _countof(szPath));
	TCHAR szTemp[_MAX_PATH];
	if (lpszFileName[0] == '\"')
		++lpszFileName;
	Checked::tcsncpy_s(szTemp, _countof(szTemp), lpszFileName, _TRUNCATE);
	LPTSTR lpszLast = _tcsrchr(szTemp, '\"');
	if (lpszLast != NULL)
		*lpszLast = 0;

	if( AfxFullPath(szPath, szTemp) == FALSE )
	{
		ASSERT(FALSE);
		return NULL; // We won't open the file. MFC requires paths with
		// length < _MAX_PATH
	}

	TCHAR szLinkName[_MAX_PATH];
	if (AfxResolveShortcut(AfxGetMainWnd(), szPath, szLinkName, _MAX_PATH))
		Checked::tcscpy_s(szPath, _countof(szPath), szLinkName);

	while (pos != NULL)
	{
		CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos);
		ASSERT_KINDOF(CDocTemplate, pTemplate);

		CDocTemplate::Confidence match;
		ASSERT(pOpenDocument == NULL);
		match = pTemplate->MatchDocType(szPath, pOpenDocument);
		if (match > bestMatch)
		{
			bestMatch = match;
			pBestTemplate = pTemplate;
		}
		if (match == CDocTemplate::yesAlreadyOpen)	break;      // stop here
	}

	if(pOpenDocument != NULL && pOpenDocument->IsKindOf(RUNTIME_CLASS(CWorkSpaceDoc)) == FALSE)
	{
		POSITION posOpenDoc = pOpenDocument->GetFirstViewPosition();
		if (posOpenDoc != NULL)
		{
			CView* pView = pOpenDocument->GetNextView(posOpenDoc); // get first one
			ASSERT_VALID(pView);
			CFrameWnd* pFrame = pView->GetParentFrame();

			if (pFrame == NULL)
				TRACE("Error: Can not find a frame for document to activate.\n");
			else
			{
				int bShow = -1;
				if(pView->IsKindOf(RUNTIME_CLASS(CWorkSpaceView))) bShow = SW_HIDE;
				pFrame->ActivateFrame(bShow);

				if (pFrame->GetParent() != NULL)
				{
					CFrameWnd* pAppFrame;
					if (pFrame != (pAppFrame = (CFrameWnd*)AfxGetApp()->m_pMainWnd))
					{
						ASSERT_KINDOF(CFrameWnd, pAppFrame);
						pAppFrame->ActivateFrame();
					}
				}
			}
		}
		else
			TRACE("Error: Can not find a view for document to activate.\n");

		return pOpenDocument;
	}/**/

	if (pBestTemplate == NULL)
	{
		AfxMessageBox(AFX_IDP_FAILED_TO_OPEN_DOC);
		return NULL;
	}

	BOOL bMakevisible = TRUE;
	if(pBestTemplate->IsKindOf(RUNTIME_CLASS(CWorkSpaceMultiDocTemplate))){
		CloseAllDocuments(TRUE);
		bMakevisible = FALSE; 
	}

	return pBestTemplate->OpenDocumentFile(szPath, bMakevisible);
	//return CDocManager::OpenDocumentFile(lpszFileName);
}

Open in new window

Commented:

Commented:
There is an intial directory field in the m_ofn (OPENFILENAME structure):
http://msdn.microsoft.com/en-us/library/ms646839(VS.85).aspx
I think, you need to set this lpstrInitialDir.
There is SetCurentDirectory function that, maybe, will help too:
http://msdn.microsoft.com/en-us/library/aa365530%28VS.85%29.aspx
 

Author

Commented:
Thanks. That pointed me in the right direction.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial