I am using the Windows API function FindFirstFileEx because it provides the capability to return just the sub-directories of a given directory (ignoring files). However when I call this function with the required flag, I still receive both files and directories.
The MSDN documentation for the FindExSearchLimitToDirectories flag used by FindFirstFileEx says:
This is an advisory flag. If the file system supports directory filtering, the function searches for a file that matches the specified name and is also a directory. If the file system does not support directory filtering, this flag is silently ignored.
The lpSearchFilter parameter of the FindFirstFileEx function must be NULL when this search value is used.
If directory filtering is desired, this flag can be used on all file systems, but because it is an advisory flag and only affects file systems that support it, the application must examine the file attribute data stored in the lpFindFileData parameter of the FindFirstFileEx function to determine whether the function has returned a handle to a directory.
So, what file systems actually support this flag? It would have been sensible to actually list these supported file systems on the same page, but I can't find it.
My development system is Windows XP SP3, NTFS, .NET 3.5.
I know I can check file attributes to determine if a file is a directory, however this means checking the every file/directory. It also defeats the purpose of using FindFirstFileEx in the first place.
Of course there is still the chance I may be doing something incorrectly in my code. The only thing I can see is passing IntPtr.Zero to lpSearchFilter may not be the same as passing NULL (as mentioned in the quote).
Here's an example of the code I'm using:
m_searchDirHandle = WinAPI.FindFirstFileEx(@"C:\Temp\*",
WinAPI.FINDEX_INFO_LEVELS.FindExInfoStandard ,
ref m_findDirData, WinAPI.FINDEX_SEARCH_OPS.FindExSearchLimitToDirectories,
IntPtr.Zero , 0);
if (m_searchDirHandle != WinAPI.INVALID_HANDLE_VALUE)
{
do
{
foundNextDir = WinAPI.FindNextFile(m_searchDirHandle, ref m_findDirData);
} while (foundNextDir);
}