try
{
// try to open the file
CFile sysFile(_T("C:\\WINDOWS\\SYSTEM.INI"), CFile::modeRead);
// print out path name and title information
_tprintf(_T("Path is : \"%s\"\n"), (LPCTSTR) sysFile.GetFilePath());
_tprintf(_T("Name is : \"%s\"\n"), (LPCTSTR) sysFile.GetFileName());
_tprintf(_T("Title is: \"%s\"\n"), (LPCTSTR) sysFile.GetFileTitle());
// close the file handle
sysFile.Close();
}
catch (CFileException* pEx)
{
// if an error occurs, just make a message box
pEx->ReportError();
pEx->Delete();
}
Output
Path is : "C:\WINDOWS\SYSTEM.INI"
Name is : "SYSTEM.INI"
Title is: "System"
方法二 : FileApi --GetFullPathName
The GetFullPathName function retrieves the full path and file name of a specified file.
DWORD GetFullPathName(
LPCTSTR lpFileName, // file name
DWORD nBufferLength, // size of path buffer
LPTSTR lpBuffer, // path buffer
LPTSTR *lpFilePart // address of file name in path
);