public static StringCollection GetPrintersCollection()
{
StringCollection printerNameCollection = new StringCollection();
string searchQuery = "SELECT * FROM Win32_Printer";
ManagementObjectSearcher searchPrinters =
new ManagementObjectSearcher(searchQuery); //exec WQL
ManagementObjectCollection printerCollection =
searchPrinters.Get(); // get a set of object of managementobject
foreach (ManagementObject printer in printerCollection)
{
if ((bool)printer.GetPropertyValue("default") == true)
//judge if the current print is the default printer
{
DefaultPrinterName = printer.Properties["Name"].Value.ToString();
}
printerNameCollection. Add(printer.Properties["Name"].Value.ToString());
//add to the string set
}
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;
}
网友评论