在MFC中,使用PathFileExists函数来检查给定的路径是否存在。该函数位于shlwapi.h头文件中。使用该函数需要传入一个LPCTSTR类型的参数,即一个指向路径字符串的指针。函数返回一个BOOL值,如果路径存在则返回TRUE,否则返回FALSE。
下面是一个使用PathFileExists函数的示例代码:
#includeBOOL IsPathExist(LPCTSTR pszPath) { return PathFileExists(pszPath); } int main() { LPCTSTR pszPath = _T("C:\\Windows\\System32\\notepad.exe"); if (IsPathExist(pszPath)) { // 路径存在 MessageBox(NULL, _T("Path exists!"), _T("Path File Exists"), MB_OK); } else { // 路径不存在 MessageBox(NULL, _T("Path does not exist!"), _T("Path File Exists"), MB_OK); } return 0; }
在上面的示例中,首先定义了一个IsPathExist函数,该函数接受一个LPCTSTR类型的参数pszPath,并通过调用PathFileExists函数来检查路径是否存在。
然后,在main函数中定义了一个路径字符串pszPath,并调用IsPathExist函数来检查该路径是否存在。根据返回的结果,显示相应的消息框。
注意:在使用PathFileExists函数之前,需要在项目的属性中链接shlwapi.lib库。