在MFC中,使用PathFileExists函數(shù)來檢查給定的路徑是否存在。該函數(shù)位于shlwapi.h頭文件中。使用該函數(shù)需要傳入一個(gè)LPCTSTR類型的參數(shù),即一個(gè)指向路徑字符串的指針。函數(shù)返回一個(gè)BOOL值,如果路徑存在則返回TRUE,否則返回FALSE。
下面是一個(gè)使用PathFileExists函數(shù)的示例代碼:
#include <shlwapi.h>
BOOL 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;
}
在上面的示例中,首先定義了一個(gè)IsPathExist函數(shù),該函數(shù)接受一個(gè)LPCTSTR類型的參數(shù)pszPath,并通過調(diào)用PathFileExists函數(shù)來檢查路徑是否存在。
然后,在main函數(shù)中定義了一個(gè)路徑字符串pszPath,并調(diào)用IsPathExist函數(shù)來檢查該路徑是否存在。根據(jù)返回的結(jié)果,顯示相應(yīng)的消息框。
注意:在使用PathFileExists函數(shù)之前,需要在項(xiàng)目的屬性中鏈接shlwapi.lib庫(kù)。