Getting the Display Name of a file Extension
-
-
Does anyone know how to get the Display Name (e.g. "Microsoft Word Document") from a given extension (e.g. "doc"). I can't use ShGetFileInfo, because that requires a file you want information for. I won't have a file, just an extention. Thanks!
-
Diarrhio wrote: I can't use ShGetFileInfo Sure you can - just use the
SHGFI_USEFILEATTRIBUTES
flag & the filename can be bogus.- Shog9 -
I'd show a smile but I'm too weak I'd share with you, could I only speak
-
Does anyone know how to get the Display Name (e.g. "Microsoft Word Document") from a given extension (e.g. "doc"). I can't use ShGetFileInfo, because that requires a file you want information for. I won't have a file, just an extention. Thanks!
Did you not read all of the documentation? It states: If the uFlags parameter includes the SHGFI_USEFILEATTRIBUTES flag, this parameter does not have to be a valid file name. The function proceeds as if the file exists with the specified name and with the file attributes passed in the dwFileAttributes parameter. This enables you to obtain information about a file type by passing just the extension for pszPath and passing FILE_ATTRIBUTE_NORMAL in dwFileAttributes. That implies that this will work:
SHFILEINFO fi; CString strExtension; m\_ebExtension.GetWindowText(strExtension); if (SHGetFileInfo(strExtension, FILE\_ATTRIBUTE\_NORMAL, &fi, sizeof(fi), SHGFI\_USEFILEATTRIBUTES | SHGFI\_TYPENAME) != 0) { m\_staticAssocApp.SetWindowText(fi.szTypeName); }
If you still do not want to use SHGetFileInfo(), try this. It's from the top of my head so you may have to tweak it:
HKEY hKey; CString strExtension; TCHAR szValue\[128\]; LONG lSize; m\_ebExtension.GetWindowText(strExtension); if (RegOpenKey(HKEY\_CLASSES\_ROOT, strExtension, &hKey) == ERROR\_SUCCESS) { lSize = sizeof(szValue); RegQueryValue(hKey, \_T(""), szValue, &lSize); RegCloseKey(hKey); if (RegOpenKey(HKEY\_CLASSES\_ROOT, szValue, &hKey) == ERROR\_SUCCESS) { lSize = sizeof(szValue); RegQueryValue(hKey, \_T(""), szValue, &lSize); RegCloseKey(hKey); m\_staticAssocApp.SetWindowText(szValue); } }