Exemplo n.º 1
0
        /// <summary>
        /// Gets the absolute path to the source folder. If not specified in the command line parameters it defaults to the working directory.
        /// </summary>
        private static string GetSourceFolder(AppConfiguration appConfig)
        {
            string sourceFolder = appConfig.SourceFolder ?? Directory.GetCurrentDirectory();

            return(Path.GetFullPath(sourceFolder));
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            IConfiguration builder = new ConfigurationBuilder()
                                     .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                     .AddCommandLine(args)
                                     .Build();

            AppConfiguration appConfig = builder.GetSection("appConfiguration").Get <AppConfiguration>();

            string sourceFolder = GetSourceFolder(appConfig);
            string outputFolder = GetOutputFolder(sourceFolder, appConfig);

            Console.Out.WriteLine($"Source folder: {sourceFolder}");
            Console.Out.WriteLine($"Output folder: {outputFolder}");

            string[] pdfFiles = Directory.GetFiles(sourceFolder, "*.pdf");
            if (pdfFiles.Length == 0)
            {
                WriteErrorToConsole("\nNo PDF files found in the source folder.");
            }
            else
            {
                Console.Out.WriteLine($"\nNumber of PDF files found in source folder: {pdfFiles.Length}");

                Directory.CreateDirectory(outputFolder);

                int successCount = 0;
                foreach (string pdfFile in pdfFiles)
                {
                    string fileName        = Path.GetFileName(pdfFile);
                    string destinationFile = Path.Combine(outputFolder, fileName);

                    bool fileCreated = false;
                    try
                    {
                        Console.Out.Write($"{fileName}... ");
                        EncryptPdfWithPassword(pdfFile, appConfig.SourcePassword,
                                               destinationFile, appConfig.UserPassword, appConfig.OwnerPassword);
                        fileCreated = true;
                        Console.Out.Write("Created... ");
                        VerifyPdf(destinationFile, appConfig.UserPassword);
                        WriteSuccessToConsole("Verified");

                        if (appConfig.DeleteSourceFile)
                        {
                            File.Delete(pdfFile);
                        }
                        successCount++;
                    }
                    catch (Exception ex)
                    {
                        if (!fileCreated)
                        {
                            WriteErrorToConsole($"Error creating file! {ex.Message}");
                        }
                        else
                        {
                            WriteErrorToConsole($"Output file not verified! {ex.Message}");
                        }

                        //might be that the destination file was created, even if empty - tidy up
                        File.Delete(destinationFile);
                    }
                }

                if (successCount == pdfFiles.Length)
                {
                    WriteSuccessToConsole("\nAll files successfully processed.");
                }
                else if (successCount == 0)
                {
                    WriteErrorToConsole("\nWarning - no files were successfully processed!");
                }
                else
                {
                    WriteErrorToConsole("\nWarning - not all files were successfully processed!");
                }
            }

            Console.Out.WriteLine("\nPress any key to close...");
            Console.ReadKey();
        }