Check if a given path is a special folder path?


On Windows, one can get any special folder path using SHGetKnownFolderPath or SHGetSpecialFolder (If I remember correctly this last one). However, I want the reverse, I have a path and want to know which special folder it belongs to, if any. I prefer this approach, because to find out if a given path's is in a particular special folder or not, I'll have to enumerate all special folders for all users which is a bit of ugly, but if there's no other way, the sky is the limit :)

I searched it but couldn't find anything useful. So does WinApi has a function to do just that?



You can use IKnownFolderManager::FindFolderFromPath

Available since Vista.

PS: check out the CComPtr<> class for simpler interfacing with COM.

Here is a sample i just made up, showing how to use it:

#include <atlsafe.h> #include <Shobjidl.h> #include <comdef.h> void PrintKnownFolder( const CComPtr<IKnownFolder>& folder ) { KNOWNFOLDER_DEFINITION def; HRESULT hr = folder->GetFolderDefinition( &def ); if( SUCCEEDED(hr) ) { std::wcout << L"Result: " << def.pszName << std::endl; FreeKnownFolderDefinitionFields( &def ); } else { _com_error err(hr); std::wcout << L"Error while querying GetFolderDefinition: " << err.ErrorMessage() << std::endl; } } class CCoInitialize { public: CCoInitialize() : m_hr(CoInitialize(NULL)) { } ~CCoInitialize() { if (SUCCEEDED(m_hr)) CoUninitialize(); } operator HRESULT() const { return m_hr; } private: HRESULT m_hr; }; bool test() { CCoInitialize co; CComPtr<IKnownFolderManager> knownFolderManager; HRESULT hr = knownFolderManager.CoCreateInstance( CLSID_KnownFolderManager ); if( !SUCCEEDED(hr) ) { _com_error err(hr); std::wcout << L"Error while creating KnownFolderManager: " << err.ErrorMessage() << std::endl; return false; } CComPtr<IKnownFolder> folder; hr = knownFolderManager->FindFolderFromPath( L"C:\\Users\\All Users\\Microsoft", FFFP_NEARESTPARENTMATCH, &folder ); if( SUCCEEDED(hr) ) { PrintKnownFolder(folder); } else { _com_error err(hr); std::wcout << L"Error while querying KnownFolderManager for nearest match: " << err.ErrorMessage() << std::endl; } // dispose it. folder.Attach( NULL ); hr = knownFolderManager->FindFolderFromPath( L"C:\\Users\\All Users\\Microsoft", FFFP_EXACTMATCH, &folder ); if( SUCCEEDED(hr) ) { PrintKnownFolder(folder); } else { _com_error err(hr); std::wcout << L"Error while querying KnownFolderManager for exact match: " << err.ErrorMessage() << std::endl; } return true; }

CCoInitialize borrowed from The Old New Thing


  • Java: can you change the value of the same string every time it loops?
  • Running Windows Speech SDK application in PHP
  • How to Implement DialogBox and onLongClick at the same time on the button?
  • Warning - Initialization makes pointer from integer without a cast
  • CountDownTimer trivia game score - Android (java)
  • Robocopy and get Copied file name
  • Simple DLL created in Visual Studio 2017 doesn't load in XP [duplicate]
  • C++ DLL returning pointer to std::list
  • COM object to create a “Open File” dialog under Vista?
  • CoInitializeEx for a boost::test::unit_test
  • what are the specifics of setting up PHP so that integers will be 64-bit?
  • Get Windows Version
  • php studio
  • Preventing executables with invalid Authenticode signatures from running
  • Windows service showing a GUI when no user is logged in
  • Development workflow for server and client using Docker Compose?
  • Accessing Windows Azure Queues from client side javascript/jquery
  • Is there a way to choose which files are displayed to the user via the standard OPENFILE dialogs?
  • How to enable large page memory for the JVM?
  • vectorized indexing/slicing in numpy/scipy?
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Recording logins for password protected directories
  • Splitting given String into two variables - php
  • Check if a string to interpolate provides expected placeholders
  • Cannot Parse HTML Data Using Android / JSOUP
  • Java static initializers and reflection
  • unknown Exception android
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?