public FileSystemArchiveFile GetOrCreateFile(string key)
        {
            var file = GetFile(key);

            if (file == null)
            {
                var info = new FileInfo(Path.Combine(owner.directoryInfo.FullName, key));
                // info.Create();
                file = new FileSystemArchiveFile(archive, owner, info);
                internalList.Add(file);
            }

            return(file);
        }
Esempio n. 2
0
        private bool TryGetFileFromFullPath(string fullPath, out FileSystemArchiveFile 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 - 1; i++)
            {
                parentDirectory = parentDirectory.Directories.GetDirectory(elements[i]);
            }

            foreach (var file in parentDirectory.Files)
            {
                if (file.Name == elements[elements.Length - 1])
                {
                    value = file;
                    return(true);
                }
            }
            value = null;
            return(false);
        }
Esempio n. 3
0
        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,
                        });
                    }
                }
            }
        }