<PackageReference Include="Relativity.Transfer.Client" Version="6.2.1" />

FileSystemService

using Relativity.Transfer.Resources; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; namespace Relativity.Transfer { public class FileSystemService : IFileSystemService { private static readonly string BackslashString; private static readonly string BackslashAltString; public string AddTrailingBackSlash(string path) { if (!PathEndsWithTrailingBackSlash(path)) path += BackslashString; return path; } public string ChangeExtension(string path, string extension) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return Path.ChangeExtension(path, extension); } public string Combine(string path1, string path2) { if (string.IsNullOrEmpty(path1)) throw new ArgumentNullException("path1"); if (string.IsNullOrEmpty(path2)) throw new ArgumentNullException("path2"); PathHelper.NormalizePath(ref path1); PathHelper.NormalizePath(ref path2); return Path.Combine(path1, path2).Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar); } public string CombineUnc(string path1, string path2) { return PathHelper.CombineUnc(path1, path2); } public void CopyFile(string sourceFile, string destinationFile, bool overwrite) { if (string.IsNullOrEmpty(sourceFile)) throw new ArgumentNullException("sourceFile"); if (string.IsNullOrEmpty(destinationFile)) throw new ArgumentNullException("destinationFile"); PathHelper.NormalizePath(ref sourceFile); string sourceFileName = PathHelper.CheckAddLongPathPrefix(sourceFile); PathHelper.NormalizePath(ref destinationFile); string destFileName = PathHelper.CheckAddLongPathPrefix(destinationFile); File.Copy(sourceFileName, destFileName, overwrite); } public void CreateDirectory(string directory) { if (string.IsNullOrEmpty(directory)) throw new ArgumentNullException("directory"); PathHelper.NormalizePath(ref directory); Directory.CreateDirectory(PathHelper.CheckAddLongPathPrefix(directory)); } public FileStream CreateNewFileStreamWithReadWriteAccess(string file) { if (string.IsNullOrEmpty(file)) throw new ArgumentNullException("file"); PathHelper.NormalizePath(ref file); string directoryName = Path.GetDirectoryName(file); if (!Directory.Exists(directoryName)) Directory.CreateDirectory(PathHelper.CheckAddLongPathPrefix(directoryName)); return new FileStream(PathHelper.CheckAddLongPathPrefix(file), FileMode.CreateNew, FileAccess.ReadWrite); } public FileStream CreateOpenFileStreamWithReadOnlyAccess(string file) { if (string.IsNullOrEmpty(file)) throw new ArgumentNullException("file"); PathHelper.NormalizePath(ref file); return new FileStream(PathHelper.CheckAddLongPathPrefix(file), FileMode.Open, FileAccess.Read); } public void DeleteDirectory(string path, bool recursive) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); Directory.Delete(PathHelper.CheckAddLongPathPrefix(path), recursive); } public void DeleteFile(string file) { if (string.IsNullOrEmpty(file)) throw new ArgumentNullException("file"); PathHelper.NormalizePath(ref file); File.Delete(PathHelper.CheckAddLongPathPrefix(file)); } public bool DirectoryExists(string directory) { if (string.IsNullOrEmpty(directory)) return false; PathHelper.NormalizePath(ref directory); return Directory.Exists(PathHelper.CheckAddLongPathPrefix(directory)); } public IEnumerable<FolderItem> EnumerateDirectories(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return new FolderItemCollection(new DirectoryInfo(PathHelper.CheckAddLongPathPrefix(path)).EnumerateDirectories()); } public IEnumerable<FolderItem> EnumerateDirectories(string path, string searchPattern) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return new FolderItemCollection(new DirectoryInfo(PathHelper.CheckAddLongPathPrefix(path)).EnumerateDirectories(searchPattern)); } public IEnumerable<FolderItem> EnumerateDirectories(string path, string searchPattern, SearchOption searchOption) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return new FolderItemCollection(new DirectoryInfo(PathHelper.CheckAddLongPathPrefix(path)).EnumerateDirectories(searchPattern, searchOption)); } public IEnumerable<FileItem> EnumerateFiles(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return new FileItemCollection(new DirectoryInfo(PathHelper.CheckAddLongPathPrefix(path)).EnumerateFiles()); } public IEnumerable<FileItem> EnumerateFiles(string path, string searchPattern) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return new FileItemCollection(new DirectoryInfo(PathHelper.CheckAddLongPathPrefix(path)).EnumerateFiles(searchPattern)); } public IEnumerable<FileItem> EnumerateFiles(string path, string searchPattern, SearchOption searchOption) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return new FileItemCollection(new DirectoryInfo(PathHelper.CheckAddLongPathPrefix(path)).EnumerateFiles(searchPattern, searchOption)); } public bool FileExists(string file) { if (string.IsNullOrEmpty(file)) return false; PathHelper.NormalizePath(ref file); return File.Exists(PathHelper.CheckAddLongPathPrefix(file)); } public FolderItem GetFolderItem(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.CheckAddLongPathPrefix(path)); return new FolderItem { FullName = PathHelper.ForceRemoveLongPathPrefix(directoryInfo.FullName), Name = directoryInfo.Name, Parent = (PathHelper.ForceRemoveLongPathPrefix(directoryInfo.Parent?.FullName) ?? string.Empty) }; } public string GetDirectoryName(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return Path.GetDirectoryName(path); } public FileItem GetFileItem(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); FileInfo fileInfo = new FileInfo(PathHelper.CheckAddLongPathPrefix(path)); return new FileItem { FolderName = PathHelper.ForceRemoveLongPathPrefix(fileInfo.DirectoryName), FullName = PathHelper.ForceRemoveLongPathPrefix(fileInfo.FullName), Length = fileInfo.Length, Name = fileInfo.Name }; } public string[] GetFiles(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return Directory.GetFiles(PathHelper.CheckAddLongPathPrefix(path)); } public string[] GetFiles(string path, string searchPattern) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return Directory.GetFiles(PathHelper.CheckAddLongPathPrefix(path), searchPattern); } public string[] GetFiles(string path, string searchPattern, SearchOption searchOption) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return Directory.GetFiles(PathHelper.CheckAddLongPathPrefix(path), searchPattern, searchOption); } public DateTime GetFileCreationTime(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return File.GetCreationTime(path); } public DateTime GetFileLastAccessTime(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return File.GetLastAccessTime(path); } public DateTime GetFileLastWriteTime(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return File.GetLastWriteTime(path); } public long GetFileLength(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); if (!FileExists(path)) throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, CoreStrings.TheFileDoesNotExistExceptionMessage, path), path); return new FileInfo(PathHelper.CheckAddLongPathPrefix(path)).Length; } public string GetFileName(string file) { if (string.IsNullOrEmpty(file)) throw new ArgumentNullException("file"); PathHelper.NormalizePath(ref file); return Path.GetFileName(file); } public string GetFileNameWithoutExtension(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return Path.GetFileNameWithoutExtension(path); } public string GetFullPath(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return Path.GetFullPath(path); } public string GetPathRoot(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return Path.GetPathRoot(path); } public string GetTempFileName() { return Path.GetTempFileName(); } public bool IsDirectoryEmpty(string directory) { if (string.IsNullOrEmpty(directory)) return false; PathHelper.NormalizePath(ref directory); return !Directory.EnumerateFileSystemEntries(PathHelper.CheckAddLongPathPrefix(directory)).Any(); } public bool IsPathRooted(string path) { if (string.IsNullOrEmpty(path)) return false; PathHelper.NormalizePath(ref path); return Path.IsPathRooted(path); } public bool IsUncPath(string path) { return PathHelper.IsUncPath(path); } public bool IsUnixPath(string path) { if (string.IsNullOrEmpty(path)) return false; return PathHelper.IsUnixPath(path); } public void MoveFile(string sourceFile, string destinationFile) { if (string.IsNullOrEmpty(sourceFile)) throw new ArgumentNullException("sourceFile"); if (string.IsNullOrEmpty(destinationFile)) throw new ArgumentNullException("destinationFile"); PathHelper.NormalizePath(ref sourceFile); string sourceFileName = PathHelper.CheckAddLongPathPrefix(sourceFile); PathHelper.NormalizePath(ref destinationFile); string destFileName = PathHelper.CheckAddLongPathPrefix(destinationFile); File.Move(sourceFileName, destFileName); } public bool PathEndsWithTrailingBackSlash(string path) { if (!string.IsNullOrEmpty(path)) { if (!path.EndsWith(BackslashString, StringComparison.OrdinalIgnoreCase)) return path.EndsWith(BackslashAltString, StringComparison.OrdinalIgnoreCase); return true; } return false; } public string ReadAllText(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); return File.ReadAllText(PathHelper.CheckAddLongPathPrefix(path)); } public void SetDirectoryCreationTime(string path, DateTime value) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); Directory.SetCreationTime(PathHelper.CheckAddLongPathPrefix(path), value); } public void SetDirectoryLastAccessTime(string path, DateTime value) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); Directory.SetLastAccessTime(PathHelper.CheckAddLongPathPrefix(path), value.ToLocalTime()); } public void SetDirectoryLastWriteTime(string path, DateTime value) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); Directory.SetLastWriteTime(PathHelper.CheckAddLongPathPrefix(path), value.ToLocalTime()); } public void SetFileCreationTime(string path, DateTime value) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); File.SetCreationTime(PathHelper.CheckAddLongPathPrefix(path), value); } public void SetFileLastAccessTime(string path, DateTime value) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); File.SetLastAccessTime(PathHelper.CheckAddLongPathPrefix(path), value.ToLocalTime()); } public void SetFileLastWriteTime(string path, DateTime value) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); File.SetLastWriteTime(PathHelper.CheckAddLongPathPrefix(path), value.ToLocalTime()); } public string TrimLeadingSlash(string path) { return PathHelper.TrimLeadingSlash(path); } public string TrimTrailingSlash(string path) { return PathHelper.TrimTrailingSlash(path); } public void WriteAllText(string path, string contents) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); PathHelper.NormalizePath(ref path); File.WriteAllText(PathHelper.CheckAddLongPathPrefix(path), contents); } static FileSystemService() { char c = Path.DirectorySeparatorChar; BackslashString = c.ToString(CultureInfo.InvariantCulture); c = Path.AltDirectorySeparatorChar; BackslashAltString = c.ToString(); } } }