2017-01-18 1 views
1

Je suis en mesure d'obtenir des informations de travail d'impression de Win32_PrintJob en utilisant WMI et ManagementEventWatcher mais je n'arrive pas à trouver le nom de l'imprimante. J'ai également regardé cette documentation Win32_PrintJob et la chose la plus proche du nom de l'imprimante est la propriété DriverName, mais il s'agit du nom du pilote d'imprimante, pas le nom de l'imprimante tel qu'affiché dans Périphériques et imprimantes du Panneau de configuration. Par conséquent, comme indiqué dans le titre, comment puis-je obtenir le nom de l'imprimante à partir du travail d'impression à partir de Win32_PrintJob?Comment obtenir le nom de l'imprimante à partir du travail d'impression à partir de Win32_PrintJob?

Ce sont mes codes partiels à ce jour pour obtenir le travail d'impression:

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

Répondre

1

Vous pouvez utiliser ce code:

// 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

Merci, je vais essayer demain comme je l'ai une autre chose à fais ce soir. –