private void FindFileEx(FileFoundEventArgs args, int slength) { _fpattern.CopyTo(_ff.Buffer, slength); _ff.Buffer[slength + _fpattern.Length] = Zero; var hFile = Kernel32.FindFirstFileEx( _ff.BufferAddress, Kernel32.FindexInfoLevels.FindExInfoBasic, out _ff.Value, Kernel32.FindexSearchOps.FindExSearchNameMatch, IntPtr.Zero, Kernel32.FindexAdditionalFlags.FindFirstExLargeFetch); if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) || (IntPtr.Size == 8 && hFile.ToInt64() == -1L)) { Win32Error(Marshal.GetLastWin32Error()); return; } try { do { var sposition = slength; for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++) { _ff.Buffer[sposition++] = _ff.Value.cFileName[ix]; } if (sposition == _ff.Buffer.Length) { throw new PathTooLongException(); } if (_ff.Value.IgnoredByName) { continue; } if ((_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory) { continue; } //got one args.SetNameOffsets(UncPrefixLength, slength, sposition); _fileFound(args); } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value)); } finally { Kernel32.FindClose(hFile); } }
private void FindFileEx(FileFoundEventArgs args, int slength) { Kernel32.FINDEX_INFO_LEVELS findInfoLevel = Kernel32.FINDEX_INFO_LEVELS.FindExInfoStandard; Kernel32.FINDEX_ADDITIONAL_FLAGS additionalFlags = 0; if (Environment.OSVersion.Version.Major >= 6) { //Ignore short-names findInfoLevel = Kernel32.FINDEX_INFO_LEVELS.FindExInfoBasic; //Use large fetch table additionalFlags = Kernel32.FINDEX_ADDITIONAL_FLAGS.FindFirstExLargeFetch; } _fpattern.CopyTo(_ff.Buffer, slength); _ff.Buffer[slength + _fpattern.Length] = ZERO; IntPtr hFile = Kernel32.FindFirstFileEx( _ff.BufferAddress, findInfoLevel, out _ff.Value, Kernel32.FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags); if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) || (IntPtr.Size == 8 && hFile.ToInt64() == -1L)) { Win32Error(Marshal.GetLastWin32Error()); return; } bool traverseDirs = _recursive && IsWild(); try { do { int sposition = slength; for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++) { _ff.Buffer[sposition++] = _ff.Value.cFileName[ix]; } if (sposition == _ff.Buffer.Length) { throw new PathTooLongException(); } if (!_ff.Value.IgnoredByName) { bool isDirectory = (_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory; if ((_includeFolders && isDirectory) || (_includeFiles && !isDirectory)) { args.SetNameOffsets(UncPrefixLength, slength, sposition); FileFound(this, args); } if (traverseDirs && isDirectory) { _ff.Buffer[sposition++] = SLASH; FindFileEx(args, sposition); } } } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value)); } finally { Kernel32.FindClose(hFile); } // Recursive search for patterns other than '*' and '*.*' requires we enum directories again if (_recursive && !traverseDirs) { _ff.Buffer[slength] = '*'; _ff.Buffer[slength + 1] = ZERO; hFile = Kernel32.FindFirstFileEx( _ff.BufferAddress, findInfoLevel, out _ff.Value, Kernel32.FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags); if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) || (IntPtr.Size == 8 && hFile.ToInt64() == -1L)) { Win32Error(Marshal.GetLastWin32Error()); return; } try { do { int sposition = slength; for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++) { _ff.Buffer[sposition++] = _ff.Value.cFileName[ix]; } if (sposition == _ff.Buffer.Length) { throw new PathTooLongException(); } if (!_ff.Value.IgnoredByName) { bool isDirectory = (_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory; if (isDirectory) { _ff.Buffer[sposition++] = SLASH; FindFileEx(args, sposition); } } } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value)); } finally { Kernel32.FindClose(hFile); } } }
private void FindFileEx(FileFoundEventArgs args, int slength) { Kernel32.FINDEX_INFO_LEVELS findInfoLevel = Kernel32.FINDEX_INFO_LEVELS.FindExInfoStandard; Kernel32.FINDEX_ADDITIONAL_FLAGS additionalFlags = 0; if (Environment.OSVersion.Version.Major >= 6) { //Ignore short-names findInfoLevel = Kernel32.FINDEX_INFO_LEVELS.FindExInfoBasic; //Use large fetch table additionalFlags = Kernel32.FINDEX_ADDITIONAL_FLAGS.FindFirstExLargeFetch; } _fpattern.CopyTo(_ff.Buffer, slength); _ff.Buffer[slength + _fpattern.Length] = ZERO; IntPtr hFile = Kernel32.FindFirstFileEx( _ff.BufferAddress, findInfoLevel, out _ff.Value, Kernel32.FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags); if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) || (IntPtr.Size == 8 && hFile.ToInt64() == -1L)) { Win32Error(Marshal.GetLastWin32Error()); return; } bool traverseDirs = _recursive && IsWild(); try { do { int sposition = slength; for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++) _ff.Buffer[sposition++] = _ff.Value.cFileName[ix]; if (sposition == _ff.Buffer.Length) throw new PathTooLongException(); if (!_ff.Value.IgnoredByName) { bool isDirectory = (_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory; if ((_includeFolders && isDirectory) || (_includeFiles && !isDirectory)) { args.SetNameOffsets(UncPrefixLength, slength, sposition); FileFound(this, args); } if (traverseDirs && isDirectory) { _ff.Buffer[sposition++] = SLASH; FindFileEx(args, sposition); } } } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value)); } finally { Kernel32.FindClose(hFile); } // Recursive search for patterns other than '*' and '*.*' requires we enum directories again if (_recursive && !traverseDirs) { _ff.Buffer[slength] = '*'; _ff.Buffer[slength + 1] = ZERO; hFile = Kernel32.FindFirstFileEx( _ff.BufferAddress, findInfoLevel, out _ff.Value, Kernel32.FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags); if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) || (IntPtr.Size == 8 && hFile.ToInt64() == -1L)) { Win32Error(Marshal.GetLastWin32Error()); return; } try { do { int sposition = slength; for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++) _ff.Buffer[sposition++] = _ff.Value.cFileName[ix]; if (sposition == _ff.Buffer.Length) throw new PathTooLongException(); if (!_ff.Value.IgnoredByName) { bool isDirectory = (_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory; if (isDirectory) { _ff.Buffer[sposition++] = SLASH; FindFileEx(args, sposition); } } } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value)); } finally { Kernel32.FindClose(hFile); } } }