BOOL PathFileExists(LPCTSTR pszPath);
Determines if a file exists.
—經檢測,該函數可以檢測文件或目錄是否存在!
Remarks
This function tests the validity of the file and path. It works only on the local file system or on a remote drive that has been mounted to a drive letter. It will return FALSE for remote file paths that begin with the UNC names //server or //server/share. It will also return FALSE if a mounted remote drive is out of service.
為了使用PathFileExists(),必須包含頭文件”shlwapi.h”,范例代碼如下:
view source
print
?
// Valid file path name (file is there). |
char buffer_1[] = "C://TEST//file.txt" ; |
// Invalid file path name (file is not there). |
char buffer_2[] = "C://TEST//file.doc" ; |
// Search for the presence of a file with a true result. |
int retval = PathFileExists(lpStr1); |
cout << "Search for the file path of : " << lpStr1 << endl; |
cout << "The file requested /"" << lpStr1 << "/" is a valid file" << endl; |
cout << "The return from function is: " << retval << endl; |
cout << "The file requested " << lpStr1 << " is not a valid file" << endl; |
cout << "The return from function is: " << retval << endl; |
// Search for the presence of a file with a false result. |
retval = PathFileExists(lpStr2); |
cout << "/nThe file requested " << lpStr2 << " is a valid file" << endl; |
cout << "Search for the file path of: " << lpStr2 << endl; |
cout << "The return from function is: " << retval << endl; |
cout << "/nThe file requested /"" << lpStr2 << "/" is not a valid file" << endl; |
cout << "The return from function is: " << retval << endl; |
編譯后,卻發現一個錯誤:error LNK2001: unresolved external symbol __imp__PathFileExistsA@4
網上搜索了下,發現是因為沒有添加相應的lib。添加lib的方法網上有不少,這里使用下面的方法:
這樣,就可以通過編譯了!