How to get a list of file type information

Adelto 1 Reputation point
2021-04-16T08:40:19.863+00:00

There are several types of files in Windows.

And directory browsers display a description of each file type.

For example, a string such as "Video File", "Audio File", "Zip Compressed File", "Text File", etc.

I need a list of descriptive strings for file types stored in Windows, using C++.

Is there a way to get a list using C++?

I know how to get the information about each file using the SHGetFileInfoW function.

But what I need is a list of information, not individual information.

The application I'm creating now needs to list all files on Windows, and needs to sort the files based on the information above.

But SHGetFileInfoW function is too slow.

So I need to get the list in advance.

Thanks for reading this text.

PS I can't speak English well.

Windows development Windows API - Win32
Developer technologies C++
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2021-04-16T21:59:32.543+00:00

    That is not as simple as you may think, as there are some
    types that are defined by standards organizations, some are
    proprietary - defined by private corporations, some that are
    purely arbitrary - such as those chosen by individual
    developers, etc. See for example:

    File Types
    https://learn.microsoft.com/en-us/windows/win32/shell/fa-file-types

    Perceived Types (The Windows Shell)
    https://learn.microsoft.com/en-us/windows/win32/shell/fa-perceivedtypes

    List of file formats
    https://en.wikipedia.org/wiki/List_of_file_formats

    While a file's extension may identify the file type

    https://www.file-extensions.org/

    note that different file types may use the same
    extension. Also, a file's actual type may only
    be identified by examining the file's contents
    such as its header data.

    It is a common trick used by malware distributors
    to give a file a false extension to hide its real
    purpose. For example trying to make an exe appear
    as a pdf file thereby luring victims into running it.

    So you are pretty much constrained to use the file
    types that are registered with Windows.

    • Wayne
    0 comments No comments

  2. Petrus 【KIM】 546 Reputation points
    2021-04-19T02:40:49.2+00:00

    Some File Extension is in the Registry (HKEY_CLASSES_ROOT)
    You can get the information like this, but not enough.

    void RetrieveFromReg()
    {
    #define MAX_KEY_LENGTH 255
        HKEY    hRootKey        = NULL;
        HKEY    hSubKey         = NULL;
    
        DWORD   dwSubKeyCount   = 0;
    
        TCHAR   szSubKey[MAX_KEY_LENGTH]    = { 0x00 };
        TCHAR   szData[MAX_KEY_LENGTH]      = { 0x00 };
    
        DWORD   dwSize          = MAX_PATH;
    
        if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CLASSES_ROOT, nullptr, 0, KEY_READ, &hRootKey))
        {
            if (ERROR_SUCCESS == RegQueryInfoKey(hRootKey, NULL, NULL, NULL, &dwSubKeyCount, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
            {
                if (dwSubKeyCount)
                {
                    for (DWORD i = 0; i < dwSubKeyCount; ++i)
                    {
                        dwSize = MAX_KEY_LENGTH;
                        if (ERROR_SUCCESS == RegEnumKeyEx(hRootKey, i, szSubKey, &dwSize, NULL, NULL, NULL, NULL))
                        {
                            if (ERROR_SUCCESS == RegOpenKeyEx(hRootKey, szSubKey, 0, KEY_READ, &hSubKey))
                            {
                                dwSize = MAX_KEY_LENGTH;
                                RegGetValue(hSubKey, NULL, NULL, RRF_RT_ANY, NULL, szData, &dwSize);
    
                                RegCloseKey(hSubKey);
                            }
                        }
                    }
                }
            }
            RegCloseKey(hRootKey);
        }
    }
    
    0 comments No comments

  3. Adelto 1 Reputation point
    2021-04-22T16:16:08.37+00:00

    I've found a way.
    Thank you all for your answers.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.