private static FindFileHandle BeginFind(string searchPattern, out WindowsNative.WIN32_FIND_DATA findData)
        {
            FindFileHandle handle = null;

            if (KtmTransaction.IsInTransaction)
            {
                handle = WindowsNative.FindFirstFileTransacted(
                    searchPattern
                    , WindowsNative.FINDEX_INFO_LEVELS.FindExInfoBasic
                    , out findData
                    , WindowsNative.FINDEX_SEARCH_OPS.FindExSearchNameMatch
                    , IntPtr.Zero
                    , 0                                 // 1 - case sensitive
                    , KtmTransaction.Current.Hanlde);
            }
            else
            {
                handle = WindowsNative.FindFirstFile(searchPattern, out findData);
            }

            if (handle.IsInvalid)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != WindowsNative.ERROR_FILE_NOT_FOUND)
                {
                    WindowsNative.HandleWindowsError(errorCode);
                }

                return(null);
            }

            return(handle);
        }
        private static IEnumerable <string> EnumerateFileSystemIterator(
            LongPath path, string mask, bool enumerateDirectories, bool enumerateFiles)
        {
            if (string.IsNullOrEmpty(mask) || mask == ".")
            {
                mask = "*";
            }
            string unprefixedPath = path.UnPrefixedPathString;

            WindowsNative.WIN32_FIND_DATA findData;
            using (FindFileHandle handle = BeginFind(Path.Combine(path.PathString, mask), out findData))
            {
                if (handle == null)
                {
                    yield break;
                }

                do
                {
                    string fileName = findData.cFileName;

                    if (IsDirectory(findData.dwFileAttributes))
                    {
                        if (enumerateDirectories && IsRealFolder(fileName))
                        {
                            yield return(Path.Combine(unprefixedPath, fileName));
                        }
                    }
                    else
                    {
                        if (enumerateFiles)
                        {
                            yield return(Path.Combine(unprefixedPath, fileName));
                        }
                    }
                }               while (WindowsNative.FindNextFile(handle, out findData));

                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != WindowsNative.ERROR_NO_MORE_FILES)
                {
                    WindowsNative.HandleWindowsError(errorCode);
                }
            }
        }
 internal static extern bool FindNextFile(FindFileHandle findFileHandle, out WIN32_FIND_DATA findFileData);