static void Main()
{
StringCollection col = GetPrintersCollection();
foreach (var printer in col)
{
Console.WriteLine(printer);
foreach (var job in GetPrintJobsCollection(printer))
{
Console.WriteLine(job);
}
}
Console.Read();
}
public static StringCollection GetPrintersCollection()
{
StringCollection printerNameCollection = new StringCollection();
string searchQuery = "SELECT * FROM Win32_Printer";
ManagementObjectSearcher searchPrinters =
new ManagementObjectSearcher(searchQuery);
ManagementObjectCollection printerCollection = searchPrinters.Get();
foreach (ManagementObject printer in printerCollection)
{
printerNameCollection.Add(printer.Properties["Name"].Value.ToString());
}
return printerNameCollection;
}
public static StringCollection GetPrintJobsCollection(string printerName)
{
StringCollection printJobCollection = new StringCollection();
string searchQuery = "SELECT * FROM Win32_PrintJob";
/*searchQuery can also be mentioned with where Attribute,
but this is not working in Windows 2000 / ME / 98 machines
and throws Invalid query error*/
ManagementObjectSearcher searchPrintJobs =
new ManagementObjectSearcher(searchQuery);
ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
foreach (ManagementObject prntJob in prntJobCollection)
{
System.String jobName = prntJob.Properties["Name"].Value.ToString();
//Job name would be of the format [Printer name], [Job ID]
char[] splitArr = new char[1];
splitArr[0] = Convert.ToChar(",");
string prnterName = jobName.Split(splitArr)[0];
string documentName = prntJob.Properties["Document"].Value.ToString();
if (String.Compare(prnterName, printerName, true) == 0)
{
printJobCollection.Add(documentName);
}
}
return printJobCollection;
}
|