3

So I am trying to create a console application that will watch a directory and when a folder is dropped into the directory it will optimize the files inside of the folder. The optimization part is working. I am currently concerned with the fact that once I run the program with the FileSystemWatcher it never recognizes the change. Is the class only focusing on the root directory and not going any deeper?

I also saw on Microsoft's website that the way to watch for a file that has been copy pasted or moved into the directory is by using FileSystemWatcher.Renamed instead of .Changed which is what I was having an issue with before.

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Runtime.CompilerServices;
using System.Threading;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;
using OptimizerTestCS;

namespace PDFNetSamples
{

    class Class1
    {


        private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();

        public static void Optimize()
        {
            Thread.Sleep(1000);
                PDFNet.Initialize();

                string input_Path = @"C:\Users\user\Desktop\testinp\";
                string output_Path = @"C:\Users\user\Desktop\output\";
                string[] files = Directory.GetFiles(input_Path, "*.pdf", SearchOption.AllDirectories);

                foreach (string file in files)
                {
                    string fileName = Path.GetFileName(file);
                    Console.WriteLine($"Optimizing {fileName}");
                    string sub = file.Substring(35, 7);
                    CreateFolder(output_Path + sub);

                    try
                    {
                        using (PDFDoc doc = new PDFDoc(file))
                        {
                            doc.InitSecurityHandler();
                            Optimizer.Optimize(doc);
                            doc.Save(output_Path + sub + fileName, SDFDoc.SaveOptions.e_linearized);
                            Console.WriteLine("Done..\n");
                        }
                    }
                    catch (PDFNetException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
        }

        private static void Run()
        {
            string[] args = Environment.GetCommandLineArgs();

            if (args.Length != 2)
            {
                Thread.Sleep(3000);
                Console.WriteLine("Usage: Watcher.exe (directory)");
                return;
            }

            using (FileSystemWatcher watcher = new FileSystemWatcher())
            {
                watcher.Path = @"C:\Users\user\Desktop\testinp\";

                watcher.NotifyFilter = NotifyFilters.LastAccess
                                       | NotifyFilters.LastWrite
                                       | NotifyFilters.FileName
                                       | NotifyFilters.DirectoryName;

                watcher.Filter = "*.pdf";

                watcher.Renamed += OnChanged;

                watcher.EnableRaisingEvents = true;

                Console.WriteLine("Press 'q' to quit the sample.");
                while (Console.Read() != 'q') ;
            }
        }

        private static void OnChanged(object source, FileSystemEventArgs e) =>
            Optimize();

    static void CreateFolder(string path)
        {
            Directory.CreateDirectory(path);
        }

        public static void Main(string[] args)
        {
            while (true)
            {
                Run();
            }
        }

    }
}

0

3 Answers 3

2

Add watcher.IncludeSubdirectories = true; to include sub directories.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to set the IncludeSubdirectories property to true.

Comments

0

Like others already said, you need to add watcher.IncludeSubdirectories = true;.

I guess your Optimize Method is not working cause of the input_path, which should be different for sub-directories as it's no longer input_path + filename. So that said, you need to adjust your input_path to match the directory which you are tracking at the moment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.