Ejemplo n.º 1
0
        /// <summary>
        /// Parse the given file path string and extract the directory path and file name.
        /// </summary>
        /// <param name="filePath">
        /// The file path string to be parsed
        /// </param>
        protected void ParseFilePath(string filePath)
        {
            // Obtain the absolute file path. Throw an exception if the path is invalid.
            string absoluteFilePath = FileOps.GetAbsoluteFilePath(filePath);

            // Parse the file path and extract the directory path
            DirectoryPath = FileOps.GetDirectoryPath(absoluteFilePath);
            // Throw an exception if the directory path is null
            if (DirectoryPath == NULL)
            {
                throw new FilePathException("Directory path can't be null")
                      {
                          FileName = FileName,
                          FilePath = NULL
                      };
            }
            // Parse the file path and extract the file name
            FileName = FileOps.GetFileName(absoluteFilePath);
            // Throw an exception if the file name is null
            if (FileName == NULL)
            {
                throw new FilePathException("File name can't be null")
                      {
                          FilePath = DirectoryPath,
                          FileName = FileName
                      };
            }
        }
Ejemplo n.º 2
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);
        }