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

Last reviewed: January 15, 1998
Article ID: Q179364
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT 4.0 - Microsoft Windows 95

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);
   }
Keywords          : UsrShell kbcode
Version           : WINNT:4.0
Platform          : Win95 winnt
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 15, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.