【问题标题】:How to Print file with using Task Scheduled如何使用计划任务打印文件
【发布时间】:2020-06-29 13:23:51
【问题描述】:

我的控制台应用程序在双击应用程序时工作,它从邮件中下载 .zip 文件,然后将 pdf 文件发送到打印机。但是,问题是当我使用计划运行此应用程序的任务时,它会读取邮件然后下载 .zip 文件,但不会将 pdf 文件发送到打印机。我不明白那是什么问题。我该如何处理?

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);
     }

顺便说一下,代码是底层工作,但我需要在上面使用

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

       pdf.LoadFromFile(path);

       pdf.Print();
       pdf.Dispose();
   }

【问题讨论】:

  • 你有 acrobat 阅读器应用吗?
  • 检查事件查看器的“应用程序日志”子部分,其中应包含记录的错误和说明,以防应用程序在执行过程中抛出异常。此外,在SendToPrinter 方法中,您从变量filePathinfo.FileName 赋值,您可能打算编写该方法的path 参数。
  • 是的,我有。正如我所说,双击时它正在工作。当应用程序由计划的任务启动时它不起作用@Clint

标签: c# scheduled-tasks


【解决方案1】:

双击应用程序时我的控制台应用程序正在运行

taskscheduler 似乎配置为在与当前登录用户不同的帐户下运行。
检查任务计划程序中配置的用户帐户以触发控制台应用程序,并确保其与您登录计算机的帐户/用户相同。

Edit Task &gt; General &gt; Security Options &gt; When running the task use the following account &gt; Select *your* account

如果您的任务是交互式(有 GUI)勾选Run only when user is logged on

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 2010-09-22
    • 2013-07-18
    • 2023-01-02
    • 1970-01-01
    • 2013-08-17
    • 2014-05-11
    相关资源
    最近更新 更多