查找目录路径

时间:2015-08-04 22:56:39

标签: c++ windows

我正在创建一个Windows应用程序,现在我可以在单击打开时找到文件的路径,但是如何才能找到目录路径?例如,如果用户选择" file.txt",那么我想获得" file.txt"。(它在磁盘上的位置)的路径。我使用的是ATL / WTL和VS 2013.

OPENFILENAME m_ofn;

BOOL m_bOpenFileDialog = true;  // TRUE for file open, FALSE for file save
TCHAR m_szFileTitle[_MAX_FNAME];// contains file title after return
TCHAR m_szFileName[_MAX_PATH];  // contains full path name after return

memset(&m_ofn, 0, sizeof(m_ofn));
m_szFileName[0] = _T('\0');
m_szFileTitle[0] = _T('\0');

m_ofn.lStructSize = sizeof(m_ofn);

m_ofn.lpstrFile = m_szFileName;
m_ofn.nMaxFile = _MAX_PATH;
m_ofn.lpstrDefExt = _T("bmp");
m_ofn.lpstrFileTitle = (LPTSTR)m_szFileTitle;
m_ofn.nMaxFileTitle = _MAX_FNAME;

m_ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | 
              OFN_OVERWRITEPROMPT | OFN_EXPLORER | 
              OFN_ENABLEHOOK | OFN_ENABLESIZING;

m_ofn.lpstrFilter = _T("Image Files (*.bmp;*.jpg;*.jpe;*.gif;*.tga)\0*.bmp;*.jpg;*.jpe;*.gif;*.tga\0All Files\0*.*\0");
m_ofn.hInstance = ModuleHelper::GetResourceInstance();
m_ofn.lpfnHook = NULL;
m_ofn.hwndOwner = NULL;

BOOL bRet;
bRet = ::GetOpenFileName(&m_ofn);
char gcd;
gcd = GetCurrentDirectory(sizeof(m_ofn), m_szFileName);
m_view.Report("Filename = %S\n", m_szFileName);

1 个答案:

答案 0 :(得分:0)

lpstrFile对象的m_ofn成员包含file.txt的完整路径。然后,您可以使用PathCchRemoveFileSpec删除尾随的\file.txt部分。