Exemplo n.º 1
0
 /// <summary>
 /// Creates a new text file if the specified file doesn't already exist
 /// </summary>
 /// <param name="filePath">The full file path of the file to be created</param>
 public virtual void CreateIfFileDoesNotExist(string filePath)
 {
     // This method should be called only if the TextFile object is in an INITIAL state.
     if (State != FileState.INITIAL)
     {
         throw new FileOpenException("File state is not INITIAL")
               {
                   FilePath = DirectoryPath,
                   FileName = FileName
               };
     }
     // Obtain the absolute file path.
     ParseFilePath(filePath);
     // Create an empty file if the specified file does not exist.
     if (!File.Exists(filePath))
     {
         FileOps.CreateFile(filePath);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Open an existing text file for writing
 /// </summary>
 /// <param name="filePath">
 /// The file path and file name of the text file to be opened
 /// </param>
 public virtual void OpenForWrite(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 doesn't exist.
     FileOps.FileMustExist(FilePath);
     // Finish opening the text file for writing
     State    = FileState.OPEN;
     Mode     = FileMode.WRITE;
     Position = 0;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Move 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 Move(string directoryPath)
        {
            // First copy the current file to the target directory
            string filePath   = Copy(directoryPath);
            string targetPath = FileOps.GetDirectoryPath(filePath);

            // Delete the file from the source location if the copy operation was successful
            try
            {
                File.Delete(FilePath);
            }
            catch (Exception e)
            {
                throw new FileOperationException("Error removing file from source location", e)
                      {
                          SourcePath = FilePath,
                          TargetPath = targetPath
                      };
            }
            DirectoryPath = targetPath;
            return(filePath);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Verify that the given directory path is valid. Create the directory if it doesn't already
        /// exist. Return the absolute directory path.
        /// </summary>
        /// <param name="directoryPath">
        /// The directory path to be validated
        /// </param>
        /// <returns>
        /// Returns the absolute directory path
        /// </returns>
        private string ValidateTargetPath(string directoryPath)
        {
            // The target path can't be null or zero length
            if (directoryPath == null || directoryPath.Length == 0)
            {
                string eFileName = NA;
                if (State == FileState.OPEN)
                {
                    eFileName = FileName;
                }
                string eFilePath = EMPTY;
                if (directoryPath == null)
                {
                    eFilePath = NULL;
                }
                throw new FilePathException("Target path is missing")
                      {
                          FileName = eFileName,
                          FilePath = eFilePath
                      };
            }
            // Verify that the target file path doesn't contain any invalid characters
            if (!FileOps.ValidDirectoryPath(directoryPath))
            {
                throw new FilePathException("Target file path contains invalid characters")
                      {
                          FilePath = directoryPath,
                          FileName = NA
                      };
            }
            // Get the full target file path
            string targetPath;

            try
            {
                targetPath = FileOps.GetAbsoluteFilePath(directoryPath);
            }
            catch (Exception e)
            {
                string eFileName = NA;
                if (State == FileState.OPEN)
                {
                    eFileName = FileName;
                }
                throw new FilePathException("Invalid target file path", e)
                      {
                          FileName = eFileName,
                          FilePath = directoryPath
                      };
            }
            // Verify that the target directory exists. Create the directory if it doesn't exist.
            if (!Directory.Exists(targetPath))
            {
                try
                {
                    Directory.CreateDirectory(targetPath);
                }
                catch (Exception e)
                {
                    string eTargetPath = targetPath;
                    if (targetPath == null)
                    {
                        eTargetPath = NULL;
                    }
                    throw new FileOperationException("Unable to create target directory", e)
                          {
                              TargetPath = eTargetPath,
                              SourcePath = NA
                          };
                }
            }
            return(targetPath);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Open an existing text file for writing
        /// </summary>
        /// <param name="directoryPath">
        /// The directory path where the file is located
        /// </param>
        /// <param name="fileName">
        /// The file name of the text file to be opened
        /// </param>
        public virtual void OpenForWrite(string directoryPath, string fileName)
        {
            string filePath = FileOps.CombinePath(directoryPath, fileName);

            OpenForWrite(filePath);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a new text file if the specified file doesn't already exist
        /// </summary>
        /// <param name="directoryPath">The directory path where the file is located</param>
        /// <param name="fileName">The file name of the text file to be created</param>
        public virtual void CreateIfFileDoesNotExist(string directoryPath, string fileName)
        {
            string filePath = FileOps.CombinePath(directoryPath, fileName);

            CreateIfFileDoesNotExist(filePath);
        }
Exemplo n.º 7
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);
        }