private void CommonInit() { Debug.Assert(_searchCriteria != null, "searchCriteria should be initialized"); // Execute searchCriteria against the current directory PathHelpers.ThrowIfEmptyOrRootedPath(_searchCriteria); string searchPath = Path.Combine(_searchData.FullPath, _searchCriteria); Interop.Kernel32.WIN32_FIND_DATA data = new Interop.Kernel32.WIN32_FIND_DATA(); using (new DisableMediaInsertionPrompt()) { // Open a Find handle _hnd = Interop.Kernel32.FindFirstFile(searchPath, ref data); if (_hnd.IsInvalid) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode != Interop.Errors.ERROR_FILE_NOT_FOUND && errorCode != Interop.Errors.ERROR_NO_MORE_FILES) { throw HandleError(errorCode, _searchData.FullPath); } else { // flag this as empty only if we're searching just top directory // Used in fast path for top directory only _empty = _searchOption == SearchOption.TopDirectoryOnly; } } } if (_searchOption == SearchOption.TopDirectoryOnly) { // fast path for TopDirectoryOnly. If we have a result, go ahead and set it to // current. If empty, dispose handle. if (_empty) { _hnd.Dispose(); } else { TSource result; if (IsResultIncluded(ref data, out result)) { current = result; } } } else { // for AllDirectories, we first recurse into dirs, so cleanup and add searchData // to the list _hnd.Dispose(); _searchList = new List <PathPair>(); _searchList.Add(_searchData); } }
private void CommonInit() { Contract.Assert(_searchCriteria != null && _searchData != null, "searchCriteria and searchData should be initialized"); // Execute searchCriteria against the current directory PathHelpers.ThrowIfEmptyOrRootedPath(_searchCriteria); String searchPath = Path.Combine(_searchData.fullPath, _searchCriteria); Interop.WIN32_FIND_DATA data = new Interop.WIN32_FIND_DATA(); // Open a Find handle _hnd = Interop.mincore.FindFirstFile(searchPath, ref data); if (_hnd.IsInvalid) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode != Interop.ERROR_FILE_NOT_FOUND && errorCode != Interop.ERROR_NO_MORE_FILES) { HandleError(errorCode, _searchData.fullPath); } else { // flag this as empty only if we're searching just top directory // Used in fast path for top directory only _empty = _searchData.searchOption == SearchOption.TopDirectoryOnly; } } // fast path for TopDirectoryOnly. If we have a result, go ahead and set it to // current. If empty, dispose handle. if (_searchData.searchOption == SearchOption.TopDirectoryOnly) { if (_empty) { _hnd.Dispose(); } else { SearchResult searchResult = CreateSearchResult(_searchData, data); if (_resultHandler.IsResultIncluded(searchResult)) { current = _resultHandler.CreateObject(searchResult); } } } // for AllDirectories, we first recurse into dirs, so cleanup and add searchData // to the stack else { _hnd.Dispose(); _searchStack.Add(_searchData); } }
internal FileSystemEnumerable( string userPath, string searchPattern, SearchOption searchOption, SearchTarget searchTarget, Func <string, bool, T> translateResult) { // Basic validation of the input path if (userPath == null) { throw new ArgumentNullException("path"); } if (string.IsNullOrWhiteSpace(userPath)) { throw new ArgumentException(SR.Argument_EmptyPath, "path"); } // Validate and normalize the search pattern. If after doing so it's empty, // matching Win32 behavior we can skip all additional validation and effectively // return an empty enumerable. searchPattern = NormalizeSearchPattern(searchPattern); if (searchPattern.Length > 0) { PathHelpers.CheckSearchPattern(searchPattern); PathHelpers.ThrowIfEmptyOrRootedPath(searchPattern); // If the search pattern contains any paths, make sure we factor those into // the user path, and then trim them off. int lastSlash = searchPattern.LastIndexOf(Path.DirectorySeparatorChar); if (lastSlash >= 0) { if (lastSlash >= 1) { userPath = Path.Combine(userPath, searchPattern.Substring(0, lastSlash)); } searchPattern = searchPattern.Substring(lastSlash + 1); } string fullPath = Path.GetFullPath(userPath); // Store everything for the enumerator _initialDirectory = new PathPair(userPath, fullPath); _searchPattern = searchPattern; _searchOption = searchOption; _includeFiles = (searchTarget & SearchTarget.Files) != 0; _includeDirectories = (searchTarget & SearchTarget.Directories) != 0; _translateResult = translateResult; } // Open the first enumerator so that any errors are propagated synchronously. _firstEnumerator = Enumerate(); }
private static string GetFullSearchString(string fullPath, string searchPattern) { Contract.Requires(fullPath != null); Contract.Requires(searchPattern != null); PathHelpers.ThrowIfEmptyOrRootedPath(searchPattern); string tempStr = Path.Combine(fullPath, searchPattern); // If path ends in a trailing slash (\), append a * or we'll get a "Cannot find the file specified" exception char lastChar = tempStr[tempStr.Length - 1]; if (PathInternal.IsDirectorySeparator(lastChar) || lastChar == Path.VolumeSeparatorChar) { tempStr = tempStr + "*"; } return(tempStr); }
[System.Security.SecurityCritical] // auto-generated private DirectoryInfo CreateSubdirectoryHelper(String path) { Debug.Assert(path != null); PathHelpers.ThrowIfEmptyOrRootedPath(path); String newDirs = Path.Combine(FullPath, path); String fullPath = Path.GetFullPath(newDirs); if (0 != String.Compare(FullPath, 0, fullPath, 0, FullPath.Length, PathInternal.StringComparison)) { throw new ArgumentException(SR.Format(SR.Argument_InvalidSubPath, path, DisplayPath), nameof(path)); } FileSystem.Current.CreateDirectory(fullPath); // Check for read permission to directory we hand back by calling this constructor. return(new DirectoryInfo(fullPath)); }
public DirectoryInfo CreateSubdirectory(string path) { if (path == null) { throw new ArgumentNullException(nameof(path)); } PathHelpers.ThrowIfEmptyOrRootedPath(path); string fullPath = Path.GetFullPath(Path.Combine(FullPath, path)); if (0 != string.Compare(FullPath, 0, fullPath, 0, FullPath.Length, PathInternal.StringComparison)) { throw new ArgumentException(SR.Format(SR.Argument_InvalidSubPath, path, FullPath), nameof(path)); } FileSystem.CreateDirectory(fullPath); return(new DirectoryInfo(fullPath)); }
internal FileSystemEnumerable( string userPath, string searchPattern, SearchOption searchOption, SearchTarget searchTarget, Func <string, bool, T> translateResult) { // Basic validation of the input path if (userPath == null) { throw new ArgumentNullException("path"); } if (string.IsNullOrEmpty(userPath)) { throw new ArgumentException(SR.Argument_EmptyPath, "path"); } // Validate and normalize the search pattern. If after doing so it's empty, // matching Win32 behavior we can skip all additional validation and effectively // return an empty enumerable. searchPattern = NormalizeSearchPattern(searchPattern); if (searchPattern.Length > 0) { PathHelpers.ThrowIfEmptyOrRootedPath(searchPattern); // If the search pattern contains any paths, make sure we factor those into // the user path, and then trim them off. int lastSlash = searchPattern.LastIndexOf(Path.DirectorySeparatorChar); if (lastSlash >= 0) { if (lastSlash >= 1) { userPath = Path.Combine(userPath, searchPattern.Substring(0, lastSlash)); } searchPattern = searchPattern.Substring(lastSlash + 1); } // Typically we shouldn't see either of these cases, an upfront check is much faster foreach (char c in searchPattern) { if (c == '\\' || c == '[') { // We need to escape any escape characters in the search pattern searchPattern = searchPattern.Replace(@"\", @"\\"); // And then escape '[' to prevent it being picked up as a wildcard searchPattern = searchPattern.Replace(@"[", @"\["); break; } } string fullPath = Path.GetFullPath(userPath); // Store everything for the enumerator _initialDirectory = new PathPair(userPath, fullPath); _searchPattern = searchPattern; _searchOption = searchOption; _includeFiles = (searchTarget & SearchTarget.Files) != 0; _includeDirectories = (searchTarget & SearchTarget.Directories) != 0; _translateResult = translateResult; } // Open the first enumerator so that any errors are propagated synchronously. _firstEnumerator = Enumerate(); }