0

My app send pdf to printer and print when I double click. However, when I want to use task scheduler. it is not printing. How can I handle this ?

static void Main(string[] args)
        {


            ExchangeService exchange = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
            exchange.Credentials = new WebCredentials("mail", "password);
            exchange.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

                if (exchange != null)
                {

                        SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
                        FindItemsResults<Item> result = exchange.FindItems(WellKnownFolderName.Inbox, sf, new ItemView(20));

                        foreach (Item item in result)
                        {

                                EmailMessage message = EmailMessage.Bind(exchange, item.Id);

                                message.IsRead = true;
                                message.Update(ConflictResolutionMode.AutoResolve);

                                List<Attachment> zipList = message.Attachments.AsEnumerable().Where(x => x.Name.Contains(".zip")).ToList();

                                foreach (Attachment Attachment in zipList)
                                {

                                        if (Attachment is FileAttachment)
                                        {
                                            FileAttachment f = Attachment as FileAttachment;

                                            f.Load("C:\\pdfFiles\\" + f.Name);



                                            string zipPath = @"C:\pdfFiles\" + f.Name;
                                            string extractPath = @"C:\pdfFiles\" + Path.GetRandomFileName();

                                            ZipFile.ExtractToDirectory(zipPath, extractPath);

                                            string[] filePaths = Directory.GetFiles(extractPath, "*.pdf",
                                                         SearchOption.TopDirectoryOnly);

                                            foreach (string path in filePaths)
                                            {

                                                    SendToPrinter(path);

                                            }


                                        }



                                }


                        }


                }

        }


  static void SendToPrinter(string path)
        {
    var printerName = "EPSON L310 Series";

         ProcessStartInfo info = new ProcessStartInfo();
                    info.Verb = "PrintTo";
                    info.FileName = filePath;
                    info.CreateNoWindow = true;
                    info.Arguments = "\"" + printerName + "\"";
                    info.WindowStyle = ProcessWindowStyle.Hidden;

                    Process p = new Process();
                    p.StartInfo = info;
                    p.Start();

                    System.Threading.Thread.Sleep(3000);
 }

By the way, code which is bottom work but I need to use above

static void SendToPrinter(string path)
        {
      PdfDocument pdf = new PdfDocument();

       pdf.LoadFromFile(path);

       pdf.Print();
       pdf.Dispose();
   }
rnn
  • 2,393
  • 3
  • 21
  • 39
  • 1
    It doesn't seem like you even need to involve any C# code at all. If you're using Scheduled Tasks, you can probably just invoke Acrobat Reader directly. See [here](https://stackoverflow.com/a/619203/3181933). – ProgrammingLlama Mar 17 '20 at 08:41
  • actually my app downloads .zip file which includes pdf from mail and open it then send printer. I didnt understand what you mean. I need to start this app with Scheduled Task @John – rnn Mar 17 '20 at 08:57
  • Is the top piece of code related to the problem you're having? – ProgrammingLlama Mar 17 '20 at 08:58
  • it is problem in just what I write on top. because second code send pdf to printer. However first one is not sending pdf to printer with using scheduled. @John – rnn Mar 17 '20 at 09:02
  • Well, I mean, both pieces of code seem to do distinctly different things. That's why I'm confused. Please can you go into more detail how these two pieces of code are related, and how you tie them together with Scheduled Tasks. – ProgrammingLlama Mar 17 '20 at 09:03
  • let me put other code @John – rnn Mar 17 '20 at 09:06
  • I added all code @John – rnn Mar 17 '20 at 09:09
  • I don't have time left to look now, but I'll take a wild guess: the scheduled task is running under a different user that doesn't have the printer installed. – ProgrammingLlama Mar 17 '20 at 09:12
  • As I said, it is work when I app when double click. it is not work when using it with task scheduled – rnn Mar 17 '20 at 09:14
  • As I said, when it is run by the task scheduler, it is probably running under a user that doesn't have the printer installed (for example: SYSTEM). If the printer isn't available to the user the application runs under as a scheduled task, then clearly it won't be able to print to it. I'd definitely look into that as a potential reason. – ProgrammingLlama Mar 17 '20 at 09:15
  • why code is bottom one is work then @John – rnn Mar 17 '20 at 09:16
  • Does the bottom one work if you run it through scheduled tasks? I thought you were saying that it didn't through scheduled tasks? – ProgrammingLlama Mar 17 '20 at 09:17
  • no bottom one works with using scheduled task. top one is not work – rnn Mar 17 '20 at 09:19
  • Does the top one work outside of a scheduled task? – ProgrammingLlama Mar 17 '20 at 09:19
  • yes, it is work – rnn Mar 17 '20 at 09:20
  • Have u tried @John – rnn Mar 17 '20 at 09:44
  • Nobody knows this issue ? – rnn Mar 18 '20 at 07:28

0 Answers0