Ejemplo n.º 1
0
        /// <summary>
        /// Rename the file
        /// </summary>
        /// <param name="fileName">
        /// New file name
        /// </param>
        public virtual void Rename(string fileName)
        {
            // Verify that the target file name is not empty or null
            if (fileName == null || fileName.Length == 0)
            {
                string eFileName = EMPTY;
                if (fileName == null)
                {
                    eFileName = NULL;
                }
                throw new FilePathException("Missing file name")
                      {
                          FilePath = DirectoryPath,
                          FileName = eFileName
                      };
            }
            // Verify that the target file name is valid
            if (fileName.IndexOf(Path.DirectorySeparatorChar) >= 0 ||
                !FileOps.ValidFileName(fileName))
            {
                throw new FilePathException("Target file name contains invalid characters")
                      {
                          FilePath = DirectoryPath,
                          FileName = fileName
                      };
            }
            // Verify that the target file name doesn't already exist
            string targetPath = Path.Combine(DirectoryPath, fileName);

            try
            {
                FileOps.FileMustNotExist(targetPath);
            }
            catch (Exception e)
            {
                throw new FileOperationException("Target file already exists", e)
                      {
                          SourcePath = FilePath,
                          TargetPath = targetPath
                      };
            }
            // Rename the file
            try
            {
                File.Move(FilePath, targetPath);
            }
            catch (Exception e)
            {
                throw new FileOperationException("Unable to rename file", e)
                      {
                          SourcePath = FilePath,
                          TargetPath = targetPath
                      };
            }
            FileName = fileName;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates a new text file for writing
 /// </summary>
 /// <param name="filePath">
 /// The file path of the text file to be opened
 /// </param>
 public virtual void CreateForWrite(string filePath)
 {
     // Check to see if the file is already open. Throw an exception if it is.
     if (State == FileState.OPEN)
     {
         throw new FileOpenException("File already open")
               {
                   FilePath = DirectoryPath,
                   FileName = FileName
               };
     }
     // Obtain the absolute file path.
     ParseFilePath(filePath);
     // Throw an exception if the file already exists.
     FileOps.FileMustNotExist(FilePath);
     // Create the new text file
     FileOps.CreateFile(FilePath);
     // Finish opening the text file for writing
     State    = FileState.OPEN;
     Mode     = FileMode.WRITE;
     Position = 0;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Copy the file to a different directory
        /// </summary>
        /// <param name="directoryPath">
        /// The target directory path
        /// </param>
        /// <returns>
        /// Returns the full file path of the target file
        /// </returns>
        public virtual string Copy(string directoryPath)
        {
            // Get the absolute directory path
            string targetPath = ValidateTargetPath(directoryPath);

            // Verify that the file is open
            if (State != FileState.OPEN)
            {
                throw new FileOpenException("File not open")
                      {
                          FileName = FileName,
                          FilePath = DirectoryPath
                      };
            }
            // Determine the full target file path
            string fullFilePath;

            try
            {
                fullFilePath = Path.Combine(targetPath, FileName);
            }
            catch (Exception e)
            {
                throw new FilePathException("Unable to determine full target file path", e)
                      {
                          FilePath = targetPath,
                          FileName = FileName
                      };
            }
            // The target file path must not match the source file path
            if (fullFilePath == FilePath)
            {
                throw new FileOperationException("Source and target are the same")
                      {
                          SourcePath = FilePath,
                          TargetPath = fullFilePath
                      };
            }
            // The target file must not already exist
            try
            {
                FileOps.FileMustNotExist(fullFilePath);
            }
            catch (Exception e)
            {
                throw new FileOpenException("Target file already exists", e)
                      {
                          FileName = FileName,
                          FilePath = targetPath
                      };
            }

            // If the source file is open for write, then save it first
            if (Mode == FileMode.WRITE)
            {
                Save();
            }
            // Copy the file to the target path
            try
            {
                File.Copy(FilePath, fullFilePath);
            }
            catch (Exception e)
            {
                throw new FileOperationException("Error copying file to target location", e)
                      {
                          SourcePath = FilePath,
                          TargetPath = fullFilePath
                      };
            }
            return(fullFilePath);
        }