【MFC】如何获取文件路径和目录

 MFC应用

一些简单获取路径、子路径、文件目录和判断路径有效的方法

// GetSubPath 从路径名中得到子路径
LPCTSTR CDirTreeCtrl::GetSubPath(LPCTSTR strPath)
{
	static CString strTemp;
	int iPos;
	strTemp = strPath;
	if ( strTemp.Right(1) == ’\\’ )
	strTemp.SetAt( strTemp.GetLength() - 1, ’\0’ );
	iPos = strTemp.ReverseFind( ’\\’ );
	if ( iPos != -1 )
	strTemp = strTemp.Mid( iPos + 1);
	return (LPCTSTR)strTemp;
}
// FindSubDir 找到子目录
BOOL CDirTreeCtrl::FindSubDir( LPCTSTR strPath)
{
	CFileFind find;
	CString strTemp = strPath;
	BOOL bFind;
	if ( strTemp[strTemp.GetLength()-1] == ’\\’ )
	strTemp += "*.*";
	else
	strTemp += "\\*.*";
	bFind = find.FindFile( strTemp );
	while ( bFind )
	{

		bFind = find.FindNextFile();
		if ( find.IsDirectory() && !find.IsDots() )
		{
		return TRUE;
		}
		if(!find.IsDirectory()&&m_bFiles && !find.IsHidden() )
		return TRUE;
	}
	return FALSE;
}
///获取全路径
CString CDirTreeCtrl::GetFullPath(HTREEITEM hItem)
{
	CString strReturn;
	CString strTemp;
	HTREEITEM hParent = hItem;
	strReturn = "";
	while ( hParent )
	{
	strTemp = GetItemText( hParent );
	strTemp += "\\";
	strReturn = strTemp + strReturn;
	hParent = GetParentItem( hParent );
	}
	strReturn.TrimRight( ’\\’ );
	return strReturn;
}
// IsValidPath 判断路径是否有效
BOOL CDirTreeCtrl::IsValidPath(LPCTSTR strPath)
{
	HTREEITEM hChild;
	CString strItem;
	CString strTempPath = strPath;
	BOOL bFound = FALSE;
	CFileFind find;
	hChild = GetChildItem( TVI_ROOT );
	strTempPath.MakeUpper();
	strTempPath.TrimRight(’\\’);
	while ( hChild )
	{
	strItem = GetItemText( hChild );
	strItem.MakeUpper();
	if ( strItem == strTempPath.Mid( 0, strItem.GetLength() ) )
	{
	bFound = TRUE;
	break;
	}
	hChild = GetNextItem( hChild, TVGN_NEXT );
	}
	if ( !bFound )
	return FALSE;
	strTempPath += "\\nul";
	if ( find.FindFile( strTempPath ) )
	return TRUE;
	return FALSE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值