internal void MoveAndRename(FileSystemArchiveDirectory parent, FileInfo fileInfo) { Parent = parent; Name = fileInfo.Name; FullName = MakeFullName(parent, fileInfo.Name); Extension = fileInfo.Extension; }
static void CopyIntoRecursive(FileSystemArchiveDirectory from, IArchiveDirectory to, string rename) { foreach (var fromFile in from.Files) { var toFile = to.Files.GetFile(fromFile.Name); if (toFile is FileSystemArchiveFile toFileSystemFile) { fromFile.FileInfo.CopyTo(toFileSystemFile.FileInfo.FullName, true); } else { using var readStream = fromFile.OpenRead(); using var writeStream = toFile.OpenWrite(); readStream.CopyTo(writeStream); } } foreach (var childFromDirectory in from.Directories) { var childToDirectory = to.Directories.GetDirectory(childFromDirectory.Name); CopyIntoRecursive(childFromDirectory, childToDirectory, childFromDirectory.Name); } }
public FileSystemArchiveFile(FileSystemArchive archive, FileSystemArchiveDirectory parent, FileInfo fileInfo) { Archive = archive; Parent = parent; FileInfo = fileInfo; Name = fileInfo.Name; FullName = MakeFullName(parent, fileInfo.Name); Extension = fileInfo.Extension; }
public FileSystemArchiveFileCollection(FileSystemArchive archive, FileSystemArchiveDirectory owner) { this.archive = archive; this.owner = owner; internalList = new List <FileSystemArchiveFile>(); foreach (var fileInfo in owner.directoryInfo .EnumerateFiles("*", SearchOption.TopDirectoryOnly)) { internalList.Add(new FileSystemArchiveFile(archive, owner, fileInfo)); } }
public FileSystemArchiveDirectory GetOrCreateDirectory(string key) { var directory = GetDirectory(key); if (directory == null) { var directoryInfo = new DirectoryInfo(Path.Combine(owner.directoryInfo.FullName, key)); directoryInfo.Create(); directory = new FileSystemArchiveDirectory(archive, owner, directoryInfo); } return(directory); }
internal FileSystemArchiveDirectory(FileSystemArchive archive, FileSystemArchiveDirectory parent, DirectoryInfo directoryInfo) { Archive = archive; Parent = parent; this.directoryInfo = directoryInfo; if (parent != null) { Name = directoryInfo.Name; FullName = MakeFullName(parent, directoryInfo.Name); } Directories = new FileSystemArchiveDirectoryCollection(archive, this); Files = new FileSystemArchiveFileCollection(archive, this); }
public FileSystemArchive(DirectoryInfo rootDirectory, bool fireEvents = true) { rootDirectory.Create(); RootDirectoryInfo = rootDirectory; RootDirectory = new FileSystemArchiveDirectory(this, null, rootDirectory); if (fireEvents) { var fileSystemWatcher = new FileSystemWatcher(rootDirectory.FullName) { IncludeSubdirectories = true }; fileSystemWatcher.Created += FileWatcherEventHandler; fileSystemWatcher.Deleted += FileWatcherEventHandler; fileSystemWatcher.Changed += FileWatcherEventHandler; fileSystemWatcher.Renamed += FileWatcherRenamedEventHandlers; fileSystemWatcher.EnableRaisingEvents = true; } }
private bool TryGetDirectoryFromFullPath(string fullPath, out FileSystemArchiveDirectory value) { string relativePath = fullPath.Substring(RootDirectoryInfo.FullName.Length + 1); string[] elements = relativePath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); var parentDirectory = RootDirectory; for (int i = 0; i < elements.Length; i++) { parentDirectory = parentDirectory.Directories.GetDirectory(elements[i]); } if (parentDirectory != null) { value = parentDirectory; return(true); } else { value = null; return(false); } }
private void FileWatcherEventHandler(object sender, FileSystemEventArgs args) { lock (watcherLock) { if (args.ChangeType.HasFlag(WatcherChangeTypes.Created)) { var attr = File.GetAttributes(args.FullPath); var parentDirectory = ParentDirectoryForEntry(args.FullPath); if (attr.HasFlag(FileAttributes.Directory)) { var newDirectory = new FileSystemArchiveDirectory(this, parentDirectory, new DirectoryInfo(args.FullPath)); parentDirectory.Directories.internalList.Add(newDirectory); OnEntryChanged?.Invoke(new ArchiveEventParameters() { ActionType = ArchiveActionType.Created, Entry = newDirectory, }); } else { var newFile = new FileSystemArchiveFile(this, parentDirectory, new FileInfo(args.FullPath)); parentDirectory.Files.internalList.Add(newFile); OnEntryChanged?.Invoke(new ArchiveEventParameters() { ActionType = ArchiveActionType.Created, Entry = newFile, }); } } if (args.ChangeType.HasFlag(WatcherChangeTypes.Changed)) { if (TryGetFileFromFullPath(args.FullPath, out var file)) { OnEntryChanged?.Invoke(new ArchiveEventParameters() { ActionType = ArchiveActionType.Changed, Entry = file, }); } } if (args.ChangeType.HasFlag(WatcherChangeTypes.Deleted)) { if (TryGetFileFromFullPath(args.FullPath, out var file)) { file.Parent.Files.internalList.Remove(file); OnEntryChanged?.Invoke(new ArchiveEventParameters() { ActionType = ArchiveActionType.Deleted, Entry = file, }); } else if (TryGetDirectoryFromFullPath(args.FullPath, out var directory)) { directory.Parent.Directories.internalList.Remove(directory); OnEntryChanged?.Invoke(new ArchiveEventParameters() { ActionType = ArchiveActionType.Deleted, Entry = directory, }); } } } }