// -------------------------------------------------------------------------------------------------------------------- public VirtualDirectoryNode CreateDirectory(string name) { _CheckDisposed(); if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException(PathUtils.NOT_NULL_EMPTY_OR_WHITESPACE_MSG, nameof(name)); } if (PathUtils.HasInvalidPathChars(name)) { throw new InvalidOperationException($"Directory name '{name}' is not valid."); } if (FileOrDirectoryExists(name, out var key)) { throw new InvalidOperationException($"A file or directory already exists with the name '{name}'."); } var dir = new VirtualDirectoryNode(this, name); _SubDirectories[key] = dir; dir.DirectoryAdded.Event += _OnSubDirectoryAdded; dir.DirectoryDeleted.Event += _OnSubDirectoryDeleted; DirectoryAdded.Raise(dir); return(dir); }
///// <summary> Gets the root watcher used to register other watchers for this file system. </summary> ///// <value> The root watcher. </value> //x public RootVirtualFileSystemWatcher RootWatcher => Root._RootWatcher; //x RootVirtualFileSystemWatcher _RootWatcher; // -------------------------------------------------------------------------------------------------------------------- /// <summary> Constructs a new directory entry. If parent is null, then this entry is considered the root entry. </summary> /// <exception cref="ArgumentException"> Thrown when 'name' is null, empty, or only whitespace. </exception> /// <param name="parent"> The parent directory. </param> /// <param name="name"> The name. </param> public VirtualDirectoryNode(VirtualDirectoryNode parent, string name) { _Parent = parent; _Name = name; FileAdded = new WeakEventHandler <VirtualDirectoryNode, VirtualFileInfo>(this); FileDeleted = new WeakEventHandler <VirtualDirectoryNode, VirtualFileInfo>(this); FileChanged = new WeakEventHandler <VirtualDirectoryNode, VirtualFileInfo>(this); }
/// <summary> /// Initializes a new instance of the CoreXT.MVC.VirtualFileProvider class using the specified volume name, hosting /// environment information, and optional base directory path. /// </summary> /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception> /// <param name="volumeName"> /// The virtual file system volume that contains the file. Volumes are stored statically in memory by this name. If the /// volume doesn't exist a new one will be created. /// </param> /// <param name="hostingEnvironment"> /// The hosting environment which is the context in which to search for content files. If provided, and a physical files exists, it will be used /// </param> /// <param name="basePath"> (Optional) Full pathname of the base file. </param> public VirtualFileProvider(string volumeName, IHostingEnvironment hostingEnvironment, string basePath = null) { VolumeName = volumeName?.Trim() ?? throw new ArgumentNullException(nameof(volumeName)); HostingEnvironment = hostingEnvironment; lock (_Volumes) { VolumeRoot = _Volumes.Value(VolumeName) ?? (_Volumes[VolumeName] = new VirtualDirectoryNode(null, "\\")); } }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> Constructor. </summary> /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception> /// <exception cref="ArgumentException"> Thrown when one or more arguments have unsupported or illegal values. </exception> /// <param name="directory"> The parent directory. </param> /// <param name="filename"> Filename of the file. </param> internal VirtualFileInfo(VirtualDirectoryNode directory, string filename) { Changed = new WeakEventHandler <VirtualFileInfo, VirtualFileInfo>(this); Disposed = new WeakEventHandler <VirtualFileInfo, VirtualFileInfo>(this); _Directory = directory ?? throw new ArgumentNullException(nameof(directory)); _Name = filename; _Data = new VirtualFileData(this); _Data.Changed += d => Changed.Raise(this); _Data.Disposed += d => Disposed.Raise(this); }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> Deletes a sub-directory and returns the deleted directory. </summary> /// <param name="dir"> The sub-directory to delete. </param> /// <returns> The deleted VirtualDirectoryNode, or null if not found. </returns> public VirtualDirectoryNode DeleteDirectory(VirtualDirectoryNode dir) { _CheckDisposed(); if (dir?.IsDisposed == false && _SubDirectories.Remove(dir.Key)) { dir.Delete(); return(dir); } else { return(null); // (not found) } }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> Gets a directory relative to the current directory using a slash-delimited path ('/' or '\' will work). </summary> /// <exception cref="InvalidOperationException"> Thrown when the requested operation is invalid. </exception> /// <param name="path"> Full path of the file directory. </param> /// <param name="createIfNotExists"> /// (Optional) True to create any missing directory entries. If false, null is returned if the directory was not found. /// </param> /// <returns> The directory if found, or null otherwise. </returns> public VirtualDirectoryNode GetDirectory(string path, bool createIfNotExists) { _CheckDisposed(); if ((path[0] == '\\' || path[0] == '/') && this != Root) { return(Root.GetDirectory(path, createIfNotExists)); // (request search at the root level; send upwards) } var names = path?.Trim().Split('/', '\\') ?? new string[0]; VirtualDirectoryNode dir = this, nextDir = null; for (var i = 0; i < names.Length; ++i) { var name = names[i].Trim(); if (!string.IsNullOrWhiteSpace(name)) { var entryKey = VirtualFileInfo.CreateKey(name); nextDir = dir._SubDirectories.Value(entryKey); if (nextDir == null) { if (createIfNotExists) { if (_Files.ContainsKey(entryKey)) { throw new InvalidOperationException($"Cannot create directory '{path}' - a directory already exists by the name '{name}' in '{dir.FullName}'."); } nextDir = new VirtualDirectoryNode(this, name) { _Key = entryKey }; _SubDirectories[entryKey] = nextDir; } else { return(null); // (not found) } } dir = nextDir; } } return(dir); }
private void _OnSubDirectoryDeleted(VirtualDirectoryNode origin, VirtualDirectoryNode dir) { DirectoryDeleted.Raise(dir); _Parent?._OnSubDirectoryDeleted(this, dir); }