Exemple #1
0
        /// <summary>
        /// Copy given file to a directory without changing it's name.
        /// Returns the full name of the destination file.
        /// </summary>
        /// <param name="sourceFilePathStr"></param>
        /// <param name="destDirectoryPath"></param>
        /// <returns></returns>
        public static FilePath CopyFile(string sourceFilePathStr, string destDirectoryPath)
        {
            FilePath destFilePath = null;

            try
            {
                FileInfo fileInfo = new FileInfo(sourceFilePathStr);
                if (fileInfo.Attributes != FileAttributes.ReadOnly)
                {
                    using (Stream fileStreamSource = new FileStream(sourceFilePathStr, FileMode.Open))
                    {
                        FilePath sourceFilePath = FilePath.CreateFilePath(sourceFilePathStr);
                        destFilePath = FilePath.CreateFilePath(destDirectoryPath, sourceFilePath.FileName);
                        StreamOperations.SaveStreamToFile(fileStreamSource, destFilePath);
                        Console.WriteLine($"{sourceFilePath.FileName} copied");
                    }
                }
                else
                {
                    Console.WriteLine("File is Read-Only, unable to copy");
                }
            }
            catch (IOException e)
            {
                Console.WriteLine($"Copying {sourceFilePathStr} failed!!!", e);
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine(e);
            }

            return(destFilePath);
        }
Exemple #2
0
        /// <summary>
        /// Duplicating a file on the same directory. Should get new name for the file.
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name=""></param>
        /// <returns></returns>
        public static FilePath DuplicateFile(FilePath originalFilePath, int timesToDuplicate)
        {
            FilePath destFilePath = null;

            try
            {
                using (Stream fileStreamSource = new FileStream(originalFilePath.FileFullPath, FileMode.Open))
                {
                    for (int i = 0; i < timesToDuplicate; ++i)
                    {
                        destFilePath = FilePath.CreateFilePathWithPrefix(originalFilePath.DirectoryPath, $"{DuplicatedPrefix}_{i}", originalFilePath.FileName);
                        StreamOperations.SaveStreamToFile(fileStreamSource, destFilePath);
                        Console.WriteLine($"{originalFilePath.FileName} duplicated to {destFilePath.FileName}");
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine($"Copying {originalFilePath} failed!!!", e);
            }

            return(destFilePath);
        }