当然根据路径获取文件名的最简单方法就是用LPTSTR PathFindFileName( LPCTSTR pPath ); 函数;
如果想跨平台,可以自己实现;
和CFileDialog差不多,不过 文件路径是直接给的:
.h文件----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#if !defined(AFX_FILENAME_H__DD386565_9863_11D1_B10E_40F603C10000__INCLUDED_)
#define AFX_FILENAME_H__DD386565_9863_11D1_B10E_40F603C10000__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
class CFileName
{
public:
CFileName(CString szFileName="");
virtual ~CFileName();
void SetFileName(CString szFileName);
CString GetFileName();
CString GetRoot();
CString GetFileTitle();
CString GetDescription();
bool Exist();
private:
CString m_szFileName;
char m_szDrive[_MAX_DRIVE];
char m_szDir[_MAX_DIR];
char m_szFname[_MAX_FNAME];
char m_szExt[_MAX_EXT];
};
#endif // !defined(AFX_FILENAME_H__DD386565_9863_11D1_B10E_40F603C10000__INCLUDED_)
.cpp文件---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "FileName.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
CFileName::CFileName(CString szFileName)
{
m_szFileName = szFileName;
}
CFileName::~CFileName()
{
}
void CFileName::SetFileName(CString szFileName)
{
m_szFileName = szFileName;
}
/*****************************************************************
*
* Function: GetFileName()
*
* Purpose: Retrieves current filename minus the path
*
* Remarks: if the filename is "c:\incoming\hello.txt", this
* function returns "hello.txt".
*
******************************************************************/
CString CFileName::GetFileName()
{
CString szFileName;
_splitpath(m_szFileName, m_szDrive, m_szDir, m_szFname, m_szExt);
szFileName = m_szFname;
szFileName += m_szExt;
return szFileName;
}
/*****************************************************************
*
* Function: GetRoot()
*
* Purpose: Retrieves the path only of the current filename.
*
* Remarks: if the filename is "c:\incoming\hello.txt", this
* function returns "c:\incoming\".
*
******************************************************************/
CString CFileName::GetRoot()
{
CString szFileName;
_splitpath(m_szFileName, m_szDrive, m_szDir, m_szFname, m_szExt);
szFileName = m_szDrive;
szFileName += m_szDir;
return szFileName;
}
/*****************************************************************
*
* Function: GetFileTitle()
*
* Purpose: Retrieves the title of the filename excluding
* the path and extension.
*
* Remarks: if the filename is "c:\incoming\hello.txt", this
* function returns "hello".
*
******************************************************************/
CString CFileName::GetFileTitle()
{
CString szFileName;
_splitpath(m_szFileName, m_szDrive, m_szDir, m_szFname, m_szExt);
szFileName = m_szFname;
return szFileName;
}
/*****************************************************************
*
* Function: GetDescription()
*
* Purpose: Returns the description of the file
*
******************************************************************/
CString CFileName::GetDescription()
{
CString szTypeName;
SHFILEINFO sfi;
SHGetFileInfo(m_szFileName,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_TYPENAME);
szTypeName = sfi.szTypeName;
return szTypeName;
}
/*****************************************************************
*
* Function: Exists()
*
* Purpose: Determines whether a file or directory exists.
*
******************************************************************/
bool CFileName::Exist()
{
WIN32_FIND_DATA fd;
CString szFindPath=m_szFileName;
int nSlash = szFindPath.ReverseFind('\\');
if( nSlash == szFindPath.GetLength()-1)
{
szFindPath = szFindPath.Left(nSlash);
}
HANDLE hFind = FindFirstFile( szFindPath, &fd );
if ( hFind != INVALID_HANDLE_VALUE )
{
FindClose( hFind );
}
return hFind != INVALID_HANDLE_VALUE;
}