3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** Class: FileSystemEnumerable
10 ** <OWNER>Microsoft</OWNER>
13 ** Purpose: Enumerates files and dirs
15 ===========================================================*/
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Security;
21 using System.Security.Permissions;
22 using Microsoft.Win32;
23 using Microsoft.Win32.SafeHandles;
25 using System.Runtime.InteropServices;
26 using System.Globalization;
27 using System.Runtime.Versioning;
28 using System.Diagnostics.Contracts;
29 using System.Threading;
35 // The key methods instantiate FileSystemEnumerableIterators. These compose the iterator with search result
36 // handlers that instantiate the FileInfo, DirectoryInfo, String, etc. The handlers then perform any
37 // additional required permission demands.
38 internal static class FileSystemEnumerableFactory
40 internal static IEnumerable<String> CreateFileNameIterator(String path, String originalUserPath, String searchPattern,
41 bool includeFiles, bool includeDirs, SearchOption searchOption, bool checkHost)
43 Contract.Requires(path != null);
44 Contract.Requires(originalUserPath != null);
45 Contract.Requires(searchPattern != null);
47 SearchResultHandler<String> handler = new StringResultHandler(includeFiles, includeDirs);
48 return new FileSystemEnumerableIterator<String>(path, originalUserPath, searchPattern, searchOption, handler, checkHost);
51 internal static IEnumerable<FileInfo> CreateFileInfoIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption)
53 Contract.Requires(path != null);
54 Contract.Requires(originalUserPath != null);
55 Contract.Requires(searchPattern != null);
57 SearchResultHandler<FileInfo> handler = new FileInfoResultHandler();
58 return new FileSystemEnumerableIterator<FileInfo>(path, originalUserPath, searchPattern, searchOption, handler, true);
61 internal static IEnumerable<DirectoryInfo> CreateDirectoryInfoIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption)
64 Contract.Requires(path != null);
65 Contract.Requires(originalUserPath != null);
66 Contract.Requires(searchPattern != null);
68 SearchResultHandler<DirectoryInfo> handler = new DirectoryInfoResultHandler();
69 return new FileSystemEnumerableIterator<DirectoryInfo>(path, originalUserPath, searchPattern, searchOption, handler, true);
72 internal static IEnumerable<FileSystemInfo> CreateFileSystemInfoIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption)
74 Contract.Requires(path != null);
75 Contract.Requires(originalUserPath != null);
76 Contract.Requires(searchPattern != null);
78 SearchResultHandler<FileSystemInfo> handler = new FileSystemInfoResultHandler();
79 return new FileSystemEnumerableIterator<FileSystemInfo>(path, originalUserPath, searchPattern, searchOption, handler, true);
83 // Abstract Iterator, borrowed from Linq. Used in anticipation of need for similar enumerables
85 abstract internal class Iterator<TSource> : IEnumerable<TSource>, IEnumerator<TSource>
89 internal TSource current;
93 threadId = Thread.CurrentThread.ManagedThreadId;
96 public TSource Current
98 get { return current; }
101 protected abstract Iterator<TSource> Clone();
103 public void Dispose()
106 GC.SuppressFinalize(this);
109 protected virtual void Dispose(bool disposing)
111 current = default(TSource);
115 public IEnumerator<TSource> GetEnumerator()
117 if (threadId == Thread.CurrentThread.ManagedThreadId && state == 0)
123 Iterator<TSource> duplicate = Clone();
128 public abstract bool MoveNext();
130 object IEnumerator.Current
132 get { return Current; }
135 IEnumerator IEnumerable.GetEnumerator()
137 return GetEnumerator();
140 void IEnumerator.Reset()
142 throw new NotSupportedException();
148 // Enumerates file system entries matching the search parameters. For recursive searches this
149 // searches through all the sub dirs and executes the search criteria against every dir.
151 // Generic implementation:
152 // FileSystemEnumerableIterator is generic. When it gets a WIN32_FIND_DATA, it calls the
153 // result handler to create an instance of the generic type.
156 // Use FileSystemEnumerableFactory to obtain FSEnumerables that can enumerate file system
157 // entries as String path names, FileInfos, DirectoryInfos, or FileSystemInfos.
160 // For all the dirs/files returned, demands path discovery permission for their parent folders
161 internal class FileSystemEnumerableIterator<TSource> : Iterator<TSource>
164 private const int STATE_INIT = 1;
165 private const int STATE_SEARCH_NEXT_DIR = 2;
166 private const int STATE_FIND_NEXT_FILE = 3;
167 private const int STATE_FINISH = 4;
169 private SearchResultHandler<TSource> _resultHandler;
170 private List<Directory.SearchData> searchStack;
171 private Directory.SearchData searchData;
172 private String searchCriteria;
173 [System.Security.SecurityCritical]
174 SafeFindHandle _hnd = null;
175 bool needsParentPathDiscoveryDemand;
177 // empty means we know in advance that we won't find any search results, which can happen if:
178 // 1. we don't have a search pattern
179 // 2. we're enumerating only the top directory and found no matches during the first call
180 // This flag allows us to return early for these cases. We can't know this in advance for
181 // SearchOption.AllDirectories because we do a "*" search for subdirs and then use the
182 // searchPattern at each directory level.
185 private String userPath;
186 private SearchOption searchOption;
187 private String fullPath;
188 private String normalizedSearchPath;
192 private bool _checkHost;
194 [System.Security.SecuritySafeCritical]
195 internal FileSystemEnumerableIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler<TSource> resultHandler, bool checkHost)
197 Contract.Requires(path != null);
198 Contract.Requires(originalUserPath != null);
199 Contract.Requires(searchPattern != null);
200 Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly);
201 Contract.Requires(resultHandler != null);
203 #if !MONO // TODO: check if we need this on Windows
204 oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS);
207 searchStack = new List<Directory.SearchData>();
209 String normalizedSearchPattern = NormalizeSearchPattern(searchPattern);
211 if (normalizedSearchPattern.Length == 0)
217 _resultHandler = resultHandler;
218 this.searchOption = searchOption;
220 fullPath = Path.GetFullPathInternal(path);
221 String fullSearchString = GetFullSearchString(fullPath, normalizedSearchPattern);
222 normalizedSearchPath = Path.GetDirectoryName(fullSearchString);
224 // permission demands
225 String[] demandPaths = new String[2];
227 // TODO: we don't call FileIOPermission.HasIllegalCharacters on Mono since CAS is disabled
229 // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters
231 demandPaths[0] = Directory.GetDemandDir(fullPath, true);
232 // For filters like foo\*.cs we need to verify if the directory foo is not denied access.
233 // Do a demand on the combined path so that we can fail early in case of deny
234 demandPaths[1] = Directory.GetDemandDir(normalizedSearchPath, true);
235 _checkHost = checkHost;
240 FileSecurityState state1 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[0]);
241 state1.EnsureState();
242 FileSecurityState state2 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[1]);
243 state2.EnsureState();
246 FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false);
250 // normalize search criteria
251 searchCriteria = GetNormalizedSearchCriteria(fullSearchString, normalizedSearchPath);
254 String searchPatternDirName = Path.GetDirectoryName(normalizedSearchPattern);
255 String userPathTemp = originalUserPath;
256 if (searchPatternDirName != null && searchPatternDirName.Length != 0)
258 userPathTemp = Path.Combine(userPathTemp, searchPatternDirName);
260 this.userPath = userPathTemp;
262 searchData = new Directory.SearchData(normalizedSearchPath, this.userPath, searchOption);
269 [System.Security.SecurityCritical]
270 private void CommonInit()
272 Contract.Assert(searchCriteria != null && searchData != null, "searchCriteria and searchData should be initialized");
274 // Execute searchCriteria against the current directory
275 String searchPath = Path.InternalCombine(searchData.fullPath, searchCriteria);
277 Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA();
279 // Open a Find handle
282 _hnd = new SafeFindHandle (MonoIO.FindFirstFile (searchPath, out data.cFileName, out data.dwFileAttributes, out error));
284 _hnd = Win32Native.FindFirstFile(searchPath, data);
292 int hr = Marshal.GetLastWin32Error();
294 if (hr != Win32Native.ERROR_FILE_NOT_FOUND && hr != Win32Native.ERROR_NO_MORE_FILES)
296 HandleError(hr, searchData.fullPath);
300 // flag this as empty only if we're searching just top directory
301 // Used in fast path for top directory only
302 empty = searchData.searchOption == SearchOption.TopDirectoryOnly;
305 // fast path for TopDirectoryOnly. If we have a result, go ahead and set it to
306 // current. If empty, dispose handle.
307 if (searchData.searchOption == SearchOption.TopDirectoryOnly)
315 SearchResult searchResult = CreateSearchResult(searchData, data);
316 if (_resultHandler.IsResultIncluded(searchResult))
318 current = _resultHandler.CreateObject(searchResult);
322 // for AllDirectories, we first recurse into dirs, so cleanup and add searchData
327 searchStack.Add(searchData);
331 [System.Security.SecuritySafeCritical]
332 private FileSystemEnumerableIterator(String fullPath, String normalizedSearchPath, String searchCriteria, String userPath, SearchOption searchOption, SearchResultHandler<TSource> resultHandler, bool checkHost)
334 this.fullPath = fullPath;
335 this.normalizedSearchPath = normalizedSearchPath;
336 this.searchCriteria = searchCriteria;
337 this._resultHandler = resultHandler;
338 this.userPath = userPath;
339 this.searchOption = searchOption;
340 this._checkHost = checkHost;
342 searchStack = new List<Directory.SearchData>();
344 if (searchCriteria != null)
346 // permission demands
347 String[] demandPaths = new String[2];
348 // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters
349 demandPaths[0] = Directory.GetDemandDir(fullPath, true);
350 // For filters like foo\*.cs we need to verify if the directory foo is not denied access.
351 // Do a demand on the combined path so that we can fail early in case of deny
352 demandPaths[1] = Directory.GetDemandDir(normalizedSearchPath, true);
357 FileSecurityState state1 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[0]);
358 state1.EnsureState();
359 FileSecurityState state2 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[1]);
360 state2.EnsureState();
363 FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false);
366 searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption);
375 protected override Iterator<TSource> Clone()
377 return new FileSystemEnumerableIterator<TSource>(fullPath, normalizedSearchPath, searchCriteria, userPath, searchOption, _resultHandler, _checkHost);
380 [System.Security.SecuritySafeCritical]
381 protected override void Dispose(bool disposing)
392 #if !MONO // TODO: check if we need this on Windows
393 Win32Native.SetErrorMode(oldMode);
395 base.Dispose(disposing);
399 [System.Security.SecuritySafeCritical]
400 public override bool MoveNext()
402 Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA();
409 state = STATE_FINISH;
410 goto case STATE_FINISH;
412 if (searchData.searchOption == SearchOption.TopDirectoryOnly)
414 state = STATE_FIND_NEXT_FILE;
421 goto case STATE_FIND_NEXT_FILE;
426 state = STATE_SEARCH_NEXT_DIR;
427 goto case STATE_SEARCH_NEXT_DIR;
430 case STATE_SEARCH_NEXT_DIR:
432 Contract.Assert(searchData.searchOption != SearchOption.TopDirectoryOnly, "should not reach this code path if searchOption == TopDirectoryOnly");
433 // Traverse directory structure. We need to get '*'
434 while (searchStack.Count > 0)
436 searchData = searchStack[0];
437 Contract.Assert((searchData.fullPath != null), "fullpath can't be null!");
438 searchStack.RemoveAt(0);
440 // Traverse the subdirs
441 AddSearchableDirsToStack(searchData);
443 // Execute searchCriteria against the current directory
444 String searchPath = Path.InternalCombine(searchData.fullPath, searchCriteria);
446 // Open a Find handle
449 _hnd = new SafeFindHandle (MonoIO.FindFirstFile (searchPath, out data.cFileName, out data.dwFileAttributes, out error));
451 _hnd = Win32Native.FindFirstFile(searchPath, data);
458 int hr = Marshal.GetLastWin32Error();
460 if (hr == Win32Native.ERROR_FILE_NOT_FOUND || hr == Win32Native.ERROR_NO_MORE_FILES || hr == Win32Native.ERROR_PATH_NOT_FOUND)
464 HandleError(hr, searchData.fullPath);
467 state = STATE_FIND_NEXT_FILE;
468 needsParentPathDiscoveryDemand = true;
469 SearchResult searchResult = CreateSearchResult(searchData, data);
470 if (_resultHandler.IsResultIncluded(searchResult))
472 if (needsParentPathDiscoveryDemand)
474 DoDemand(searchData.fullPath);
475 needsParentPathDiscoveryDemand = false;
477 current = _resultHandler.CreateObject(searchResult);
482 goto case STATE_FIND_NEXT_FILE;
485 state = STATE_FINISH;
486 goto case STATE_FINISH;
488 case STATE_FIND_NEXT_FILE:
490 if (searchData != null && _hnd != null)
492 // Keep asking for more matching files/dirs, add it to the list
495 while (MonoIO.FindNextFile (_hnd.DangerousGetHandle(), out data.cFileName, out data.dwFileAttributes, out error))
497 while (Win32Native.FindNextFile(_hnd, data))
500 SearchResult searchResult = CreateSearchResult(searchData, data);
501 if (_resultHandler.IsResultIncluded(searchResult))
503 if (needsParentPathDiscoveryDemand)
505 DoDemand(searchData.fullPath);
506 needsParentPathDiscoveryDemand = false;
508 current = _resultHandler.CreateObject(searchResult);
516 // Make sure we quit with a sensible error.
517 int hr = Marshal.GetLastWin32Error();
523 // ERROR_FILE_NOT_FOUND is valid here because if the top level
524 // dir doen't contain any subdirs and matching files then
525 // we will get here with this errorcode from the searchStack walk
526 if ((hr != 0) && (hr != Win32Native.ERROR_NO_MORE_FILES)
527 && (hr != Win32Native.ERROR_FILE_NOT_FOUND))
529 HandleError(hr, searchData.fullPath);
532 if (searchData.searchOption == SearchOption.TopDirectoryOnly)
534 state = STATE_FINISH;
535 goto case STATE_FINISH;
539 state = STATE_SEARCH_NEXT_DIR;
540 goto case STATE_SEARCH_NEXT_DIR;
552 [System.Security.SecurityCritical]
553 private SearchResult CreateSearchResult(Directory.SearchData localSearchData, Win32Native.WIN32_FIND_DATA findData)
555 String userPathFinal = Path.InternalCombine(localSearchData.userPath, findData.cFileName);
556 String fullPathFinal = Path.InternalCombine(localSearchData.fullPath, findData.cFileName);
557 return new SearchResult(fullPathFinal, userPathFinal, findData);
560 [System.Security.SecurityCritical]
561 private void HandleError(int hr, String path)
564 __Error.WinIOError(hr, path);
567 [System.Security.SecurityCritical] // auto-generated
568 private void AddSearchableDirsToStack(Directory.SearchData localSearchData)
570 Contract.Requires(localSearchData != null);
572 String searchPath = Path.InternalCombine(localSearchData.fullPath, "*");
573 SafeFindHandle hnd = null;
574 Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA();
577 // Get all files and dirs
580 hnd = new SafeFindHandle (MonoIO.FindFirstFile (searchPath, out data.cFileName, out data.dwFileAttributes, out error));
582 hnd = Win32Native.FindFirstFile(searchPath, data);
590 int hr = Marshal.GetLastWin32Error();
593 // This could happen if the dir doesn't contain any files.
594 // Continue with the recursive search though, eventually
595 // searchStack will become empty
596 if (hr == Win32Native.ERROR_FILE_NOT_FOUND || hr == Win32Native.ERROR_NO_MORE_FILES || hr == Win32Native.ERROR_PATH_NOT_FOUND)
599 HandleError(hr, localSearchData.fullPath);
602 // Add subdirs to searchStack. Exempt ReparsePoints as appropriate
606 if (FileSystemEnumerableHelpers.IsDir(data))
608 String tempFullPath = Path.InternalCombine(localSearchData.fullPath, data.cFileName);
609 String tempUserPath = Path.InternalCombine(localSearchData.userPath, data.cFileName);
611 SearchOption option = localSearchData.searchOption;
613 #if EXCLUDE_REPARSEPOINTS
614 // Traverse reparse points depending on the searchoption specified
615 if ((searchDataSubDir.searchOption == SearchOption.AllDirectories) && (0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_REPARSE_POINT)))
616 option = SearchOption.TopDirectoryOnly;
618 // Setup search data for the sub directory and push it into the stack
619 Directory.SearchData searchDataSubDir = new Directory.SearchData(tempFullPath, tempUserPath, option);
621 searchStack.Insert(incr++, searchDataSubDir);
624 } while (MonoIO.FindNextFile (hnd.DangerousGetHandle(), out data.cFileName, out data.dwFileAttributes, out error));
626 } while (Win32Native.FindNextFile(hnd, data));
628 // We don't care about errors here
637 [System.Security.SecurityCritical]
638 internal void DoDemand(String fullPathToDemand)
643 String demandDir = Directory.GetDemandDir(fullPathToDemand, true);
644 FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandDir);
648 String demandDir = Directory.GetDemandDir(fullPathToDemand, true);
649 FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, demandDir, false, false);
654 private static String NormalizeSearchPattern(String searchPattern)
656 Contract.Requires(searchPattern != null);
658 // Win32 normalization trims only U+0020.
659 String tempSearchPattern = searchPattern.TrimEnd(Path.TrimEndChars);
661 // Make this corner case more useful, like dir
662 if (tempSearchPattern.Equals("."))
664 tempSearchPattern = "*";
667 Path.CheckSearchPattern(tempSearchPattern);
668 return tempSearchPattern;
671 private static String GetNormalizedSearchCriteria(String fullSearchString, String fullPathMod)
673 Contract.Requires(fullSearchString != null);
674 Contract.Requires(fullPathMod != null);
675 Contract.Requires(fullSearchString.Length >= fullPathMod.Length);
677 String searchCriteria = null;
678 char lastChar = fullPathMod[fullPathMod.Length - 1];
679 if (Path.IsDirectorySeparator(lastChar))
681 // Can happen if the path is C:\temp, in which case GetDirectoryName would return C:\
682 searchCriteria = fullSearchString.Substring(fullPathMod.Length);
686 Contract.Assert(fullSearchString.Length > fullPathMod.Length);
687 searchCriteria = fullSearchString.Substring(fullPathMod.Length + 1);
689 return searchCriteria;
692 private static String GetFullSearchString(String fullPath, String searchPattern)
694 Contract.Requires(fullPath != null);
695 Contract.Requires(searchPattern != null);
697 String tempStr = Path.InternalCombine(fullPath, searchPattern);
699 // If path ends in a trailing slash (\), append a * or we'll get a "Cannot find the file specified" exception
700 char lastChar = tempStr[tempStr.Length - 1];
701 if (Path.IsDirectorySeparator(lastChar) || lastChar == Path.VolumeSeparatorChar)
703 tempStr = tempStr + '*';
710 internal abstract class SearchResultHandler<TSource>
713 [System.Security.SecurityCritical]
714 internal abstract bool IsResultIncluded(SearchResult result);
716 [System.Security.SecurityCritical]
717 internal abstract TSource CreateObject(SearchResult result);
721 internal class StringResultHandler : SearchResultHandler<String>
723 private bool _includeFiles;
724 private bool _includeDirs;
726 internal StringResultHandler(bool includeFiles, bool includeDirs)
728 _includeFiles = includeFiles;
729 _includeDirs = includeDirs;
732 [System.Security.SecurityCritical]
733 internal override bool IsResultIncluded(SearchResult result)
735 bool includeFile = _includeFiles && FileSystemEnumerableHelpers.IsFile(result.FindData);
736 bool includeDir = _includeDirs && FileSystemEnumerableHelpers.IsDir(result.FindData);
737 Contract.Assert(!(includeFile && includeDir), result.FindData.cFileName + ": current item can't be both file and dir!");
738 return (includeFile || includeDir);
741 [System.Security.SecurityCritical]
742 internal override String CreateObject(SearchResult result)
744 return result.UserPath;
748 internal class FileInfoResultHandler : SearchResultHandler<FileInfo>
750 [System.Security.SecurityCritical]
751 internal override bool IsResultIncluded(SearchResult result)
753 return FileSystemEnumerableHelpers.IsFile(result.FindData);
756 [System.Security.SecurityCritical]
757 internal override FileInfo CreateObject(SearchResult result)
759 String name = result.FullPath;
762 FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, String.Empty, name);
765 FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, name, false, false);
768 FileInfo fi = new FileInfo(name, false);
769 fi.InitializeFrom(result.FindData);
774 internal class DirectoryInfoResultHandler : SearchResultHandler<DirectoryInfo>
776 [System.Security.SecurityCritical]
777 internal override bool IsResultIncluded(SearchResult result)
779 return FileSystemEnumerableHelpers.IsDir(result.FindData);
782 [System.Security.SecurityCritical]
783 internal override DirectoryInfo CreateObject(SearchResult result)
785 String name = result.FullPath;
786 String permissionName = name + "\\.";
790 FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, String.Empty, permissionName);
793 FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, permissionName, false, false);
796 DirectoryInfo di = new DirectoryInfo(name, false);
797 di.InitializeFrom(result.FindData);
802 internal class FileSystemInfoResultHandler : SearchResultHandler<FileSystemInfo>
805 [System.Security.SecurityCritical]
806 internal override bool IsResultIncluded(SearchResult result)
808 bool includeFile = FileSystemEnumerableHelpers.IsFile(result.FindData);
809 bool includeDir = FileSystemEnumerableHelpers.IsDir(result.FindData);
810 Contract.Assert(!(includeFile && includeDir), result.FindData.cFileName + ": current item can't be both file and dir!");
812 return (includeDir || includeFile);
815 [System.Security.SecurityCritical]
816 internal override FileSystemInfo CreateObject(SearchResult result)
818 bool isFile = FileSystemEnumerableHelpers.IsFile(result.FindData);
819 bool isDir = FileSystemEnumerableHelpers.IsDir(result.FindData);
823 String name = result.FullPath;
824 String permissionName = name + "\\.";
828 FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, String.Empty, permissionName);
831 FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, permissionName, false, false);
834 DirectoryInfo di = new DirectoryInfo(name, false);
835 di.InitializeFrom(result.FindData);
840 Contract.Assert(isFile);
841 String name = result.FullPath;
845 FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, String.Empty, name);
848 FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, name, false, false);
851 FileInfo fi = new FileInfo(name, false);
852 fi.InitializeFrom(result.FindData);
859 internal sealed class SearchResult
861 private String fullPath; // fully-qualifed path
862 private String userPath; // user-specified path
863 [System.Security.SecurityCritical]
864 private Win32Native.WIN32_FIND_DATA findData;
866 [System.Security.SecurityCritical]
867 internal SearchResult(String fullPath, String userPath, Win32Native.WIN32_FIND_DATA findData)
869 Contract.Requires(fullPath != null);
870 Contract.Requires(userPath != null);
872 this.fullPath = fullPath;
873 this.userPath = userPath;
874 this.findData = findData;
877 internal String FullPath
879 get { return fullPath; }
882 internal String UserPath
884 get { return userPath; }
887 internal Win32Native.WIN32_FIND_DATA FindData
889 [System.Security.SecurityCritical]
890 get { return findData; }
895 internal static class FileSystemEnumerableHelpers
897 [System.Security.SecurityCritical] // auto-generated
898 internal static bool IsDir(Win32Native.WIN32_FIND_DATA data)
900 // Don't add "." nor ".."
902 return (0 != (data.dwFileAttributes & (int)Win32Native.FILE_ATTRIBUTE_DIRECTORY))
904 return (0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY))
906 && !data.cFileName.Equals(".") && !data.cFileName.Equals("..");
909 [System.Security.SecurityCritical] // auto-generated
910 internal static bool IsFile(Win32Native.WIN32_FIND_DATA data)
913 return 0 == (data.dwFileAttributes & (int)Win32Native.FILE_ATTRIBUTE_DIRECTORY);
915 return 0 == (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY);