【问题标题】:Optimizer won't stop after end new files found by FSW结束 FSW 找到的新文件后优化器不会停止
【发布时间】:2020-01-27 18:27:12
【问题描述】:

我创建了一个 CLI 应用程序,它将监视一个目录并优化移入其中的任何新 PDF。截至我上次构建时没有错误。

我遇到的问题是,当我运行它时,应用程序会检测到更改并优化更改的文件,但它不会停止优化新文件的循环。

一旦优化过程到达新文件的末尾,我将如何设置停止点?

public class Methods
        {
            [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
            public static void Optimize()
            {
                Thread.Sleep(1000);
                PDFNet.Initialize();

                string input_Path = @"C:\Users\user\Desktop\testinpactive\";
                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(41, 7);
                    CreateFolder(output_Path + sub);

                    // CreateFolder(output_Path + );
                    try
                    {
                        using (PDFDoc doc = new PDFDoc(file))
                        {
                            //--------------------------------------------------------------------------------
                            // Example 1) Simple optimization of a pdf with default settings.
                            doc.InitSecurityHandler();
                            Optimizer.Optimize(doc);
                            doc.Save(output_Path + sub + fileName, SDFDoc.SaveOptions.e_linearized);
                            // File Delete Process
                            //File.Delete(input_Path + files);
                            //Console.WriteLine("File Deleted");
                            Console.WriteLine("Done..\n");
                        }
                    }
                    catch (PDFNetException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }

            public static void Run()
            {
                // Create a new FileSystemWatcher and set its properties.
                // Params: Path, and filter
                using (FileSystemWatcher watcher = new FileSystemWatcher(@"C:\Users\user\Desktop\testinpactive", "*.pdf"))
                {
                    // To watch SubDirectories 
                    watcher.IncludeSubdirectories = true;

                    FswHandler Handler = new FswHandler();

                    // Add event handlers.
                    watcher.Created += Handler.OnCreated;

                    // Begin watching.
                    watcher.EnableRaisingEvents = true;

                    // Wait for the user to quit the program.
                    Console.WriteLine("Press 'q' to quit the sample.");
                    while (Console.Read() != 'q');
                }
            }
            public class FswHandler
            {
                public void OnCreated(Object source, FileSystemEventArgs e)
                {
                    // Write out Path (Testing)
                    Console.WriteLine($"FILE: {e.FullPath} CHANGE-TYPE: {e.ChangeType}");
                    // Specify what is done when a file is changed, created, or deleted.
                    Optimize();
                }

            }

【问题讨论】:

  • 现在,当任何文件发生更改时,您正在优化所有文件。 FileSystemEventArgs 中应该有一个文件名。
  • @BenVoigt 那么最好的解决方案是从 Optimize 方法中删除 foreach 并将其设置为优化 1 个文件名为 FileSystemEventArgs 的文件吗?
  • 这当然是一种选择。另一种方法是在处理每个文件时重命名(到目录外)。很大程度上取决于您希望如何处理程序未运行时出现的文件。

标签: c# filesystemwatcher


【解决方案1】:

非常感谢 @BenVoigt 解释我做错了什么。

这是我为解决问题而对代码所做的更改。

public static void Optimize(string filePath, string outputPath, string fileName, string fileNum)
            {
                PDFNet.Initialize();
                try
                {
                        using (PDFDoc doc = new PDFDoc(filePath)) 
                        {
                            //--------------------------------------------------------------------------------
                            // Example 1) Simple optimization of a pdf with default settings.
                            doc.InitSecurityHandler();
                            Optimizer.Optimize(doc);
                            Directory.CreateDirectory(outputPath + filePath.Substring(41, 7));
                            doc.Save(filePath, SDFDoc.SaveOptions.e_linearized);
                            //doc.Save(outputPath + fileNum + fileName, SDFDoc.SaveOptions.e_linearized);
                            Console.WriteLine("Done..\n");
                        }
                }
                catch (PDFNetException e)
                {
                        Console.WriteLine(e.Message);
                }
            }

            public static void Run()
            {
                // Create a new FileSystemWatcher and set its properties.
                // Params: Path, and filter
                using (FileSystemWatcher watcher = new FileSystemWatcher(@"C:\Users\user\Desktop\testinpactive", "*.pdf"))
                {
                    // To watch SubDirectories 
                    watcher.IncludeSubdirectories = true;

                    FswHandler Handler = new FswHandler();

                    // Add event handlers.
                    watcher.Created += Handler.OnCreated;

                    // Begin watching.
                    watcher.EnableRaisingEvents = true;

                    // Wait for the user to quit the program.
                    Console.WriteLine("Press 'q' to quit the sample.");
                    while (Console.Read() != 'q');
                }
            }
            public class FswHandler
            {
                public void OnCreated(Object source, FileSystemEventArgs e)
                {
                    string output = @"C:\Users\user\Desktop\output";
                    // Write out Path (Testing)
                    Console.WriteLine($"FILE: {e.FullPath} CHANGE-TYPE: {e.ChangeType}");
                    Thread.Sleep(800);
                    // Specify what is done when a file is changed, created, or deleted.
                    Optimize(e.FullPath, output, e.Name.Substring(7), e.FullPath.Substring(40, 8));
                }

            }

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多