共用方式為


CFile::GetFileName

virtual CString GetFileName( ) const;

Return Value

The name of the file.

Remarks

Call this member function to retrieve the name of a specified file. For example, when you call GetFileName to generate a message to the user about the file c:\windows\write\myfile.wri, the filename, myfile.wri, is returned.

To return the entire path of the file, including the name, call GetFilePath. To return the title of the file (myfile), call GetFileTitle.

Example

This code fragment opens the SYSTEM.INI file in your WINDOWS directory. If found, the example will print out the name and path and title, as shown under Output:

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"

CFile OverviewClass MembersHierarchy Chart

See Also   CFile::GetFilePath, CFile::GetFileTitle