CFileName

当然根据路径获取文件名的最简单方法就是用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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值