【发布时间】: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方法中,您从变量filePath为info.FileName赋值,您可能打算编写该方法的path参数。 -
是的,我有。正如我所说,双击时它正在工作。当应用程序由计划的任务启动时它不起作用@Clint
标签: c# scheduled-tasks