Моя консольная программа работает при двойном щелчке приложения, загружает 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();
}