您的位置:首页 > 其它

PathFileExists用法--使用#include

2011-05-09 14:22 531 查看
BOOLPathFileExists(LPCTSTRpszPath);

Determinesifafileexists.

---经检测,该函数可以检测文件或目录是否存在

Remarks

Thisfunctionteststhevalidityofthefileandpath.Itworksonlyonthelocalfilesystemoronaremotedrivethathasbeenmountedtoadriveletter.ItwillreturnFALSEforremotefilepathsthatbeginwiththeUNCnames//serveror//server/share.ItwillalsoreturnFALSEifamountedremotedriveisoutofservice.

为了使用PathFileExists(),必须包含头文件"shlwapi.h",范例代码如下:

viewsourceprint?

#include<windows.h>
#include<iostream.h>
#include<shlwapi.h>
void
main(
void
)
{
//Validfilepathname(fileisthere).
char
buffer_1[]=
"C://TEST//file.txt"
;
char
*lpStr1;
lpStr1=buffer_1;
//Invalidfilepathname(fileisnotthere).
char
buffer_2[]=
"C://TEST//file.doc"
;
char
*lpStr2;
lpStr2=buffer_2;
//Searchforthepresenceofafilewithatrueresult.
int
retval=PathFileExists(lpStr1);
if
(retval==1)
{
cout<<
"Searchforthefilepathof:"
<<lpStr1<<endl;
cout<<
"Thefilerequested/""
<<lpStr1<<
"/"isavalidfile"
<<endl;
cout<<
"Thereturnfromfunctionis:"
<<retval<<endl;
}
else
{
cout<<
"Thefilerequested"
<<lpStr1<<
"isnotavalidfile"
<<endl;
cout<<
"Thereturnfromfunctionis:"
<<retval<<endl;
}
//Searchforthepresenceofafilewithafalseresult.
retval=PathFileExists(lpStr2);
if
(retval==1)
{
cout<<
"/nThefilerequested"
<<lpStr2<<
"isavalidfile"
<<endl;
cout<<
"Searchforthefilepathof:"
<<lpStr2<<endl;
cout<<
"Thereturnfromfunctionis:"
<<retval<<endl;
}
else
{
cout<<
"/nThefilerequested/""
<<lpStr2<<
"/"isnotavalidfile"
<<endl;
cout<<
"Thereturnfromfunctionis:"
<<retval<<endl;
}
}
编译后,却发现一个错误:errorLNK2001:unresolvedexternalsymbol__imp__PathFileExistsA@4

网上搜索了下,发现是因为没有添加相应的lib。添加lib的方法网上有不少,这里使用下面的方法:





这样,就可以通过编译了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: