// This function is used to add folders to the hierarchy from the root. public Folder AddFolder(string folderName, string folderId, string folderParentId, FolderType folderType) { if (id != "0") { throw new Exception("This function is only valid on root folders."); } // Check if a folder with this ID already exists Folder existingFolder = FindFolderById(folderId); if (existingFolder != null) { // If a folder with that Id, name, parent, and type already exist, // just return it instead of creating a new one. if (existingFolder.Name == folderName && existingFolder.ParentFolder.Id == folderParentId && existingFolder.Type == folderType) { return(existingFolder); } else { // There should never be two folders with the same Id, so // throw an exception. string exceptionString = string.Format( "A folder with the Id = {0} already exists. Name = {1}, ParentId = {2}, Type = {3}.", folderId, existingFolder.Name, existingFolder.ParentFolder.Id, existingFolder.Type); throw new ArgumentException(exceptionString); } } // The folder does not already exist, so find the parent. Folder parentFolder = FindFolderById(folderParentId); if (parentFolder == null) { throw new Exception(string.Format("Invalid parent folder Id: {0}", folderParentId)); } // Add a new subfolder to the parent. return(parentFolder.AddSubFolder(folderName, folderId, folderType)); }
// This function updates an existing folder by either // renaming it or moving it. public void Update(string folderName, Folder newParent, Folder.FolderType folderType) { // Set the new name name = folderName; // Check to see if we are moving the folder. if (newParent != parentFolder) { // Remove this folder from the current parent's // subfolders parentFolder.RemoveSubFolder(this); parentFolder = newParent; // Add this folder to the new parent's subfolders newParent.AddSubFolder(this); } // Call MoveLocalFolder to move the local data to a new // directory. MoveLocalFolder(newParent.SaveLocation + "\\" + name); }