Bloom.Book.BookStorage.SetBookName C# (CSharp) Méthode

SetBookName() public méthode

public SetBookName ( string name ) : void
name string
Résultat void
        public void SetBookName(string name)
        {
            if (!Directory.Exists(_folderPath)) //bl-290 (user had 4 month-old version, so the bug may well be long gone)
            {
                var msg = LocalizationManager.GetString("BookStorage.FolderMoved",
                    "It appears that some part of the folder path to this book has been moved or renamed. As a result, Bloom cannot save your changes to this page, and will need to exit now. If you haven't been renaming or moving things, please click Details below and report the problem to the developers.");
                SIL.Reporting.ErrorReport.NotifyUserOfProblem(
                    new ApplicationException(
                        string.Format(
                            "In SetBookName('{0}'), BookStorage thinks the existing folder is '{1}', but that does not exist. (ref bl-290)",
                            name, _folderPath)),
                    msg);
                // Application.Exit() is not drastic enough to terminate all the call paths here and all the code
                // that tries to make sure we save on exit. Get lots of flashing windows during shutdown.
                Environment.Exit(-1);
            }
            name = SanitizeNameForFileSystem(name);

            var currentFilePath = PathToExistingHtml;
            //REVIEW: This doesn't immediately make sense; if this function is told to call it Foo but it's current Foo1... why does this just return?

            if (Path.GetFileNameWithoutExtension(currentFilePath).StartsWith(name)) //starts with because maybe we have "myBook1"
                return;

            //figure out what name we're really going to use (might need to add a number suffix)
            var newFolderPath = Path.Combine(Directory.GetParent(FolderPath).FullName, name);
            newFolderPath = GetUniqueFolderPath(newFolderPath);

            Logger.WriteEvent("Renaming html from '{0}' to '{1}.htm'", currentFilePath, newFolderPath);

            //next, rename the file
            RobustFile.Move(currentFilePath, Path.Combine(FolderPath, Path.GetFileName(newFolderPath) + ".htm"));

            //next, rename the enclosing folder
            var fromToPair = new KeyValuePair<string, string>(FolderPath, newFolderPath);
            try
            {
                Logger.WriteEvent("Renaming folder from '{0}' to '{1}'", FolderPath, newFolderPath);

                //This one can't handle network paths and isn't necessary, since we know these are on the same volume:
                //SIL.IO.DirectoryUtilities.MoveDirectorySafely(FolderPath, newFolderPath);
                SIL.IO.RobustIO.MoveDirectory(FolderPath, newFolderPath);

                _fileLocator.RemovePath(FolderPath);
                _fileLocator.AddPath(newFolderPath);

                _folderPath = newFolderPath;
            }
            catch (Exception e)
            {
                Logger.WriteEvent("Failed folder rename: " + e.Message);
                Debug.Fail("(debug mode only): could not rename the folder");
            }

            _bookRenamedEvent.Raise(fromToPair);

            OnFolderPathChanged();
        }

Usage Example

 private void ChangeNameAndCheck(TemporaryFolder newFolder, BookStorage storage)
 {
     var newBookName = Path.GetFileName(newFolder.Path);
     storage.SetBookName(newBookName);
     var newPath = newFolder.Combine(newBookName + ".htm");
     Assert.IsTrue(Directory.Exists(newFolder.Path), "Expected folder:" + newFolder.Path);
     Assert.IsTrue(File.Exists(newPath), "Expected file:" + newPath);
 }
All Usage Examples Of Bloom.Book.BookStorage::SetBookName