2017-01-18 92 views
1

WMIとManagementEventWatcherを使用してWin32_PrintJobから印刷ジョブ情報を取得できますが、プリンタ名が見つからないようです。私もこのWin32_PrintJobドキュメントを見て、プリンタ名に最も近いのはDriverNameプロパティですが、それはコントロールパネルのデバイスとプリンタに表示されるプリンタ名ではなく、プリンタドライバ名です。Win32_PrintJobから印刷ジョブからプリンタ名を取得するには?

タイトルに記載されているように、印刷ジョブからプリンタ名を取得するには、どうすればWin32_PrintJobになりますか?

これが印刷ジョブを取得するために、これまでの私の部分のコードです:

public void PrintHelperInstance_OnPrintJobChange(object sender, EventArrivedEventArgs e) 
{ 
    ManagementBaseObject objProps = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value; 

    string jobName = objProps["Document"].ToString(); 

    if (jobName == "Test Print Form") 
    { 
     if (!IsFoundPrintJob) 
     { 
      IsFoundPrintJob = true; 
     } 

     CurrentJobStatus = (string)objProps["JobStatus"]; 

     if (CurrentJobStatus != PreviousJobStatus) 
     { 
      uint jobId = (uint)objProps["JobId"]; 
      string jobPrinter = (string)objProps["DriverName"]; 
      string jobHost = (string)objProps["HostPrintQueue"]; 
      string jobStatus = (string)objProps["JobStatus"]; 

      PreviousJobStatus = CurrentJobStatus; 
     } 
    } 
} 

答えて

1

あなたはこのコードを使用することができます:

// produce wmi query object 
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Printer); 
// produce search object 
ManagementObjectSearcher search = new ManagementObjectSearcher(quer); 
// retrieve result collection 
ManagementObjectCollection restul = search.Get(); 
// iterate through all printers 
foreach(ManagementObject obj in result) 
{ 
    // now create your temp printer class 
    Dictionary<string, object> printerObj = new Dictionary<string, object>(); 
    if(obj.GetPropertyValue("Local").ToString().Equals("true")) 
    { 
     printerObj.Add("isLocal", true); 
     printerObj.Add("name", obj.GetPropertyValue("name").ToString()); 
    } 
    else 
    { 
     printerObj.Add("isLocal", false); 
     printerObj.Add("serverName", obj.GetPropertyValue("ServerName").ToString()); 
     printerObj.Add("shareName", obj.GetPropertyValue("ShareName").ToString()); 
    } 

    // create real printer object 
    PrintServer srv = ((bool)printerObj["isLocal")) ? new LocalPrintServer() : new PrintServer((string)printerObj["serverName"]); 
    PrintQueue queue = srv.GetPrintQueue(((bool)printerObj["isLocal")) ? (string)printerObj["name"] : (string)printerObj["shareName"]; 

    foreach(var job in queue.GetPrintJobInfoCollection()) 
    { 
     // check job info and if it matches, return printer name; 
    } 
} 
+0

をありがとう私が別のものを持っているように、私はこの明日しようとします今夜はやる。 –

関連する問題