public void SetDirectoryPath(string directoryPath) { if (directoryPath != null) { this.FilePath = LongPath.Combine(directoryPath, this.RelativePath); } }
private FileLocation(SerializationInfo info, StreamingContext context) { if (info == null) { throw new System.ArgumentNullException("info"); } if (context.Context is StreamJournal && "RelativePath".Equals(info.GetString(FilePathType))) { this.RelativePath = info.GetString(FilePathName); this.FilePath = LongPath.Combine(((StreamJournal)context.Context).DirectoryPath, this.RelativePath); } else { this.RelativePath = null; this.FilePath = info.GetString(FilePathName); } }
public static IEnumerable <string> EnumerateFileSystemEntries(string path, string searchPattern, SearchOption searchOption, FilesOrDirectory filter) { #if DOTNET5_4 return(Directory.EnumerateFileSystemEntries(path, searchPattern, searchOption)); #else NativeMethods.WIN32_FIND_DATA findData; NativeMethods.SafeFindHandle findHandle; string currentPath = null; int errorCode = 0; Queue <string> folders = new Queue <string>(); String searchPath = LongPath.Combine(path, searchPattern); path = LongPath.GetDirectoryName(searchPath); searchPattern = LongPath.GetFileName(searchPath); folders.Enqueue(path); while (folders.Count > 0) { currentPath = folders.Dequeue(); if (searchOption == SearchOption.AllDirectories) { findHandle = NativeMethods.FindFirstFileW(LongPath.Combine(LongPath.ToUncPath(currentPath), "*"), out findData); if (!findHandle.IsInvalid) { do { if (findData.FileName != "." && findData.FileName != "..") { if (findData.FileAttributes == FileAttributes.Directory) { folders.Enqueue(LongPath.Combine(currentPath, findData.FileName)); } } }while (NativeMethods.FindNextFileW(findHandle, out findData)); // Get last Win32 error right after native calls. // Dispose SafeFindHandle will call native methods, it is possible to set last Win32 error. errorCode = Marshal.GetLastWin32Error(); if (findHandle != null && !findHandle.IsInvalid) { findHandle.Dispose(); } NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(errorCode, new int[] { NativeMethods.ERROR_SUCCESS, NativeMethods.ERROR_NO_MORE_FILES, NativeMethods.ERROR_FILE_NOT_FOUND }); } else { NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(new int[] { NativeMethods.ERROR_SUCCESS, NativeMethods.ERROR_NO_MORE_FILES, NativeMethods.ERROR_FILE_NOT_FOUND }); } } findHandle = NativeMethods.FindFirstFileW(LongPath.Combine(LongPath.ToUncPath(currentPath), searchPattern), out findData); if (!findHandle.IsInvalid) { do { if (findData.FileName != "." && findData.FileName != "..") { if ((filter == FilesOrDirectory.All) || (filter == FilesOrDirectory.Directory && findData.FileAttributes == FileAttributes.Directory) || (filter == FilesOrDirectory.File && findData.FileAttributes != FileAttributes.Directory)) { yield return(LongPath.Combine(currentPath, findData.FileName)); } } }while (NativeMethods.FindNextFileW(findHandle, out findData)); // Get last Win32 error right after native calls. // Dispose SafeFindHandle will call native methods, it is possible to set last Win32 error. errorCode = Marshal.GetLastWin32Error(); if (findHandle != null && !findHandle.IsInvalid) { findHandle.Dispose(); } NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(errorCode, new int[] { NativeMethods.ERROR_SUCCESS, NativeMethods.ERROR_NO_MORE_FILES, NativeMethods.ERROR_FILE_NOT_FOUND }); } else { NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(new int[] { NativeMethods.ERROR_SUCCESS, NativeMethods.ERROR_NO_MORE_FILES, NativeMethods.ERROR_FILE_NOT_FOUND }); } } #endif }