public static string Combine(string path1, string path2) { if (path1 == null || path2 == null) { throw new ArgumentNullException(path1 == null ? "path1" : "path2"); } Path.CheckInvalidPathChars(path1, false); Path.CheckInvalidPathChars(path2, false); return(Path.CombineNoChecks(path1, path2)); }
/// <summary>将四个字符串组合成一个路径。</summary> /// <returns>已组合的路径。</returns> /// <param name="path1">要组合的第一个路径。</param> /// <param name="path2">要组合的第二个路径。</param> /// <param name="path3">要组合的第三个路径。</param> /// <param name="path4">要组合的第四个路径。</param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="path1" />、<paramref name="path2" />、<paramref name="path3" /> 或 <paramref name="path4" /> 包含 <see cref="M:System.IO.Path.GetInvalidPathChars" /> 中已定义的一个或多个无效字符。</exception> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="path1" />、<paramref name="path2" />、<paramref name="path3" /> 或 <paramref name="path4" /> 为 null。</exception> public static string Combine(string path1, string path2, string path3, string path4) { if (path1 == null || path2 == null || (path3 == null || path4 == null)) { throw new ArgumentNullException(path1 == null ? "path1" : (path2 == null ? "path2" : (path3 == null ? "path3" : "path4"))); } Path.CheckInvalidPathChars(path1, false); Path.CheckInvalidPathChars(path2, false); Path.CheckInvalidPathChars(path3, false); Path.CheckInvalidPathChars(path4, false); return(Path.CombineNoChecks(Path.CombineNoChecks(Path.CombineNoChecks(path1, path2), path3), path4)); }
internal static FileInfo CreateFileInfo(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData) { string cFileName = findData.cFileName; string text = Path.CombineNoChecks(searchData.fullPath, cFileName); if (!CodeAccessSecurityEngine.QuickCheckForAllDemands()) { new FileIOPermission(FileIOPermissionAccess.Read, new string[] { text }, false, false).Demand(); } FileInfo fileInfo = new FileInfo(text, cFileName); fileInfo.InitializeFrom(ref findData); return(fileInfo); }
internal static DirectoryInfo CreateDirectoryInfo(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData) { string fileName = findData.cFileName; string fullPath = Path.CombineNoChecks(searchData.fullPath, fileName); if (!CodeAccessSecurityEngine.QuickCheckForAllDemands()) { // There is no need to emulate checks that FileIOPermission does if we aren't in full trust. // The paths we're getting are already tested and/or coming straight from the OS. new FileIOPermission(FileIOPermissionAccess.Read, new string[] { fullPath + "\\." }, false, false).Demand(); } DirectoryInfo di = new DirectoryInfo(fullPath, fileName); di.InitializeFrom(ref findData); return(di); }
internal FileSystemEnumerableIterator(string path, string originalUserPath, string searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost) { this.oldMode = Win32Native.SetErrorMode(1); this.searchStack = new List <Directory.SearchData>(); string text = FileSystemEnumerableIterator <TSource> .NormalizeSearchPattern(searchPattern); if (text.Length == 0) { this.empty = true; return; } this._resultHandler = resultHandler; this.searchOption = searchOption; this.fullPath = Path.GetFullPathInternal(path); string fullSearchString = FileSystemEnumerableIterator <TSource> .GetFullSearchString(this.fullPath, text); this.normalizedSearchPath = Path.GetDirectoryName(fullSearchString); if (CodeAccessSecurityEngine.QuickCheckForAllDemands()) { FileIOPermission.EmulateFileIOPermissionChecks(this.fullPath); FileIOPermission.EmulateFileIOPermissionChecks(this.normalizedSearchPath); } else { new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[] { Directory.GetDemandDir(this.fullPath, true), Directory.GetDemandDir(this.normalizedSearchPath, true) }, false, false).Demand(); } this._checkHost = checkHost; this.searchCriteria = FileSystemEnumerableIterator <TSource> .GetNormalizedSearchCriteria(fullSearchString, this.normalizedSearchPath); string directoryName = Path.GetDirectoryName(text); string path2 = originalUserPath; if (directoryName != null && directoryName.Length != 0) { path2 = Path.CombineNoChecks(path2, directoryName); } this.userPath = path2; this.searchData = new Directory.SearchData(this.normalizedSearchPath, this.userPath, searchOption); this.CommonInit(); }
private void AddSearchableDirsToStack(Directory.SearchData localSearchData) { string fileName = Path.InternalCombine(localSearchData.fullPath, "*"); SafeFindHandle safeFindHandle = null; Win32Native.WIN32_FIND_DATA win32_FIND_DATA = default(Win32Native.WIN32_FIND_DATA); try { safeFindHandle = Win32Native.FindFirstFile(fileName, ref win32_FIND_DATA); if (safeFindHandle.IsInvalid) { int lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error == 2 || lastWin32Error == 18 || lastWin32Error == 3) { return; } this.HandleError(lastWin32Error, localSearchData.fullPath); } int num = 0; do { if (win32_FIND_DATA.IsNormalDirectory) { string cFileName = win32_FIND_DATA.cFileName; string text = Path.CombineNoChecks(localSearchData.fullPath, cFileName); string text2 = Path.CombineNoChecks(localSearchData.userPath, cFileName); SearchOption searchOption = localSearchData.searchOption; Directory.SearchData item = new Directory.SearchData(text, text2, searchOption); this.searchStack.Insert(num++, item); } }while (Win32Native.FindNextFile(safeFindHandle, ref win32_FIND_DATA)); } finally { if (safeFindHandle != null) { safeFindHandle.Dispose(); } } }
internal override string CreateObject(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData) { return(Path.CombineNoChecks(searchData.userPath, findData.cFileName)); }
[System.Security.SecurityCritical] // auto-generated private void AddSearchableDirsToStack(Directory.SearchData localSearchData) { Contract.Requires(localSearchData != null); String searchPath = Path.InternalCombine(localSearchData.fullPath, "*"); SafeFindHandle hnd = null; Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA(); try { // Get all files and dirs hnd = Win32Native.FindFirstFile(searchPath, ref data); if (hnd.IsInvalid) { int hr = Marshal.GetLastWin32Error(); // This could happen if the dir doesn't contain any files. // Continue with the recursive search though, eventually // searchStack will become empty if (hr == Win32Native.ERROR_FILE_NOT_FOUND || hr == Win32Native.ERROR_NO_MORE_FILES || hr == Win32Native.ERROR_PATH_NOT_FOUND) { return; } HandleError(hr, localSearchData.fullPath); } // Add subdirs to searchStack. Exempt ReparsePoints as appropriate int incr = 0; do { if (data.IsNormalDirectory) { string fileName = data.cFileName; string tempFullPath = Path.CombineNoChecks(localSearchData.fullPath, fileName); string tempUserPath = Path.CombineNoChecks(localSearchData.userPath, fileName); SearchOption option = localSearchData.searchOption; #if EXCLUDE_REPARSEPOINTS // Traverse reparse points depending on the searchoption specified if ((searchDataSubDir.searchOption == SearchOption.AllDirectories) && (0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_REPARSE_POINT))) { option = SearchOption.TopDirectoryOnly; } #endif // Setup search data for the sub directory and push it into the stack Directory.SearchData searchDataSubDir = new Directory.SearchData(tempFullPath, tempUserPath, option); searchStack.Insert(incr++, searchDataSubDir); } } while (Win32Native.FindNextFile(hnd, ref data)); // We don't care about errors here } finally { if (hnd != null) { hnd.Dispose(); } } }
internal FileSystemEnumerableIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost) { Contract.Requires(path != null); Contract.Requires(originalUserPath != null); Contract.Requires(searchPattern != null); Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly); Contract.Requires(resultHandler != null); oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS); searchStack = new List <Directory.SearchData>(); String normalizedSearchPattern = NormalizeSearchPattern(searchPattern); if (normalizedSearchPattern.Length == 0) { empty = true; } else { _resultHandler = resultHandler; this.searchOption = searchOption; fullPath = Path.GetFullPathInternal(path); String fullSearchString = GetFullSearchString(fullPath, normalizedSearchPattern); normalizedSearchPath = Path.GetDirectoryName(fullSearchString); if (CodeAccessSecurityEngine.QuickCheckForAllDemands()) { // Full trust, just need to validate incoming paths // (we don't need to get the demand directory as it has no impact) FileIOPermission.EmulateFileIOPermissionChecks(fullPath); FileIOPermission.EmulateFileIOPermissionChecks(normalizedSearchPath); } else { // Not full trust, need to check for rights string[] demandPaths = new string[2]; // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters demandPaths[0] = Directory.GetDemandDir(fullPath, true); // For filters like foo\*.cs we need to verify if the directory foo is not denied access. // Do a demand on the combined path so that we can fail early in case of deny demandPaths[1] = Directory.GetDemandDir(normalizedSearchPath, true); new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false).Demand(); } _checkHost = checkHost; // normalize search criteria searchCriteria = GetNormalizedSearchCriteria(fullSearchString, normalizedSearchPath); // fix up user path String searchPatternDirName = Path.GetDirectoryName(normalizedSearchPattern); String userPathTemp = originalUserPath; if (searchPatternDirName != null && searchPatternDirName.Length != 0) { userPathTemp = Path.CombineNoChecks(userPathTemp, searchPatternDirName); } this.userPath = userPathTemp; searchData = new Directory.SearchData(normalizedSearchPath, this.userPath, searchOption); CommonInit(); } }