怎样检测目录的有效性?

元明 2003-01-10 09:23:54
我需要用户手动输入路径,我怎样从edit中验证目录的有效性?
...全文
15 点赞 收藏 5
写回复
5 条回复
元明 2003年01月27日
To ccnuxjg,你的代码给了崭新的思路,特此感谢!
回复 点赞
ccnuxjg 2003年01月10日
To use _access function you must include <io.h> header file.
In some cases in order to use strpbrk function you must include <string.h> header file.


BOOL MakeSurePathExists( CString &Path,
bool FilenameIncluded=true)
{
int Pos=0;
while((Pos=Path.Find('\\',Pos+1))!=-1)
CreateDirectory(Path.Left(Pos),NULL);
if(!FilenameIncluded)
CreateDirectory(Path,NULL);
return ((!FilenameIncluded)?!_access(Path,0):
!_access(Path.Left(Path.ReverseFind('\\')),0));
}

int make_sure_path_exists( const char *iPath,
bool FilenameIncluded=true)
{
char *Path=(char*)iPath,
*TmpPath=Path,
TmpSmb=0,
*LastDPtr=NULL;
while((TmpPath=strpbrk(TmpPath+1,"\\/")))
{
TmpSmb=Path[TmpPath-Path];
Path[TmpPath-Path]=0;
CreateDirectory(Path,NULL);
Path[TmpPath-Path]=TmpSmb;
LastDPtr=TmpPath;
}

int Res=1;
if(!FilenameIncluded)
{
CreateDirectory(iPath,NULL);
Res=!_access(iPath,0);
}
else
{
if(LastDPtr)
{
Path=(char*)iPath;
TmpSmb=Path[LastDPtr-Path];
Path[LastDPtr-Path]=0;
Res=!_access(Path,0);
Path[LastDPtr-Path]=TmpSmb;
}
}

return Res;
}




#include "shlwapi.h"
BOOL PathFileExists(
LPCTSTR pszPath
);

and make sure that you link to
shlwapi.lib in your lib settings

回复 点赞
LinHanLao 2003年01月10日
BOOL PathFileExists(
LPCTSTR lpszPath
);
回复 点赞
ccnuxjg 2003年01月10日
To use _access function you must include <io.h> header file.
In some cases in order to use strpbrk function you must include <string.h> header file.


BOOL MakeSurePathExists( CString &Path,
bool FilenameIncluded=true)
{
int Pos=0;
while((Pos=Path.Find('\\',Pos+1))!=-1)
CreateDirectory(Path.Left(Pos),NULL);
if(!FilenameIncluded)
CreateDirectory(Path,NULL);
return ((!FilenameIncluded)?!_access(Path,0):
!_access(Path.Left(Path.ReverseFind('\\')),0));
}

int make_sure_path_exists( const char *iPath,
bool FilenameIncluded=true)
{
char *Path=(char*)iPath,
*TmpPath=Path,
TmpSmb=0,
*LastDPtr=NULL;
while((TmpPath=strpbrk(TmpPath+1,"\\/")))
{
TmpSmb=Path[TmpPath-Path];
Path[TmpPath-Path]=0;
CreateDirectory(Path,NULL);
Path[TmpPath-Path]=TmpSmb;
LastDPtr=TmpPath;
}

int Res=1;
if(!FilenameIncluded)
{
CreateDirectory(iPath,NULL);
Res=!_access(iPath,0);
}
else
{
if(LastDPtr)
{
Path=(char*)iPath;
TmpSmb=Path[LastDPtr-Path];
Path[LastDPtr-Path]=0;
Res=!_access(Path,0);
Path[LastDPtr-Path]=TmpSmb;
}
}

return Res;
}




#include "shlwapi.h"
BOOL PathFileExists(
LPCTSTR pszPath
);

and make sure that you link to
shlwapi.lib in your lib settings

回复 点赞
vc_love 2003年01月10日
FindFile
回复 点赞
发动态
发帖子
VC/MFC
创建于2007-09-28

7898

社区成员

42.1w+

社区内容

VC/MFC相关问题讨论
社区公告
暂无公告