[英]LocalPrintServer.GetDefaultPrintQueue() does not work in windows service
我想在用戶打印所有內容時彈出一個消息框。 以下代碼可以很好地完成工作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Printing;
using System.Management;
using System.Windows.Forms;
namespace CatchByWMI
{
class Program
{
static String prevJobName;
static void Main(string[] args)
{
String strComputerName = "localhost";
// Create event query to be notified within 1 second of
// a change in a service
WqlEventQuery query = new WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 0.1 WHERE TargetInstance ISA \"Win32_PrintJob\"");
ManagementEventWatcher watcher = new ManagementEventWatcher();
watcher.Scope = new ManagementScope("\\\\" + strComputerName + "\\root\\CIMV2");
watcher.Query = query;
// times out watcher.WaitForNextEvent in 5 seconds
watcher.Options.Timeout = new TimeSpan(0, 0, 5);
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
// Start listening
watcher.Start();
// Do something in the meantime
System.Threading.Thread.Sleep(100000);
// Stop listening
watcher.Stop();
}
private static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
try
{
PrintQueue printQueue = LocalPrintServer.GetDefaultPrintQueue();
PrintJobInfoCollection coll = printQueue.GetPrintJobInfoCollection();
String n = Environment.NewLine;
foreach (PrintSystemJobInfo job in coll)
{
if (!job.Name.Equals(prevJobName))
{
job.Pause();
Console.WriteLine(job.Name);
// MB_TOPMOST
MessageBox.Show("hello", "Header", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000);
job.Resume();
prevJobName = job.Name;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
但是,當我將其轉換為Windows服務時,它會提示以下錯誤消息:
獲取默認打印機時發生異常。 Win32錯誤系統找不到指定的文件。
如果提供任何解決方案,我將不勝感激。
這是可行的代碼:
try
{
WqlObjectQuery sql = new WqlObjectQuery("SELECT * FROM Win32_PrintJob");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(sql);
foreach (ManagementObject printJob in searcher.Get())
{
if (!prevJobName.Equals(printJob["Name"]))
{
Console.WriteLine(printJob["Name"] + ":" + printJob["Status"]);
printJob.InvokeMethod("Pause", null);
prevJobName = (String)printJob["Name"];
Interop.ShowMessageBox("Click Ok To continue", "MyService Message");
printJob.InvokeMethod("Resume", null);
}
}
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.ToString());
}
Interop.cs源代碼(此對象用於彈出消息):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace PrintJobMonitor
{
class Interop
{
public static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
public static void ShowMessageBox(string message, string title)
{
int resp = 0;
WTSSendMessage(
WTS_CURRENT_SERVER_HANDLE,
WTSGetActiveConsoleSessionId(),
title, title.Length,
message, message.Length,
0, 0, out resp, true);
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int WTSGetActiveConsoleSessionId();
[DllImport("wtsapi32.dll", SetLastError = true)]
public static extern bool WTSSendMessage(
IntPtr hServer,
int SessionId,
String pTitle,
int TitleLength,
String pMessage,
int MessageLength,
int Style,
int Timeout,
out int pResponse,
bool bWait);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.