HOWTO: Determine if File Extension Should Be Shown for a File

ID: Q179364

The information in this article applies to:

SUMMARY

Windows 95 and Windows NT do not provide any way to determine if a user has globally turned off the display of file extensions in Windows Explorer. To find out if an extension should be displayed on a file-by-file basis, you can use the APIs GetFileTitle() or SHGetFileInfo() to get the proper display name of a file. In either case, the API will give the title of the file properly formatted for display. If the user has requested that file extensions be displayed for that file type, then it will have an extension. Otherwise, it will not.

NOTE: If you have Internet Explorer 4.0 or greater installed, you can use the SHGetSettings API to determine if the user has specified that file extensions be displayed. However, if your application is displaying file names, you should use the method outlined in this article to get the proper display name for a file.

MORE INFORMATION

The following code demonstrates how to use GetFileTitle() to retrieve the appropriate display name of a file:

Sample Code

   LPTSTR lpszTitle;
   WORD cbBuf;
   short nErr;

   // Find the length needed for the title (including null terminator).
   // lpszFile should contain the name and location of the file.
   cbBuf = GetFileTitle(lpszFile,NULL,0);
   if (cbBuf < 0) {
      // There was an error with lpszFile.
   }
   lpszTitle = (LPTSTR) GlobalAlloc(GPTR, sizeof(TCHAR)* cbBuf);

   if (lpszTitle) {
      nErr =  GetFileTitle(lpszFile,lpszTitle,cbBuf);
      if (0 == nErr) {
         // lpszTitle now has the appropriate display name of the
         // file, including or excluding the extension per the
         // user's preferences.
         DoStuffWithTheTitle(lpszTitle);
      } else {
         // There was an error.
         if (nErr < 0) {
            // error in lpszFile's format.
         } else {
            // nErr > 0 means lpszTitle was too small a buffer.
         }
      }
      GlobalFree(lpszTitle);
   }

Additional query words:
Keywords          : kbcode kbLib kbNTOS400 kbWinOS2000 kbWinOS95 kbWinOS98 kbGrpShell 
Issue type        : kbhowto

Last Reviewed: December 17, 1998