In the standard PrintDialog there are four values associated with a selected printer: Status, Type, Where, and Comment.
If I know a printer's name, how can I get these values in C# 2.0?
In the standard PrintDialog there are four values associated with a selected printer: Status, Type, Where, and Comment.
If I know a printer's name, how can I get these values in C# 2.0?
As dowski suggested, you could use WMI to get printer properties. The following code displays all properties for a given printer name. Among them you will find: PrinterStatus, Comment, Location, DriverName, PortName, etc.
using System.Management;
...
string printerName = "YourPrinterName";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
using (ManagementObjectCollection coll = searcher.Get())
{
try
{
foreach (ManagementObject printer in coll)
{
foreach (PropertyData property in printer.Properties)
{
Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
}
}
}
catch (ManagementException ex)
{
Console.WriteLine(ex.Message);
}
}
This should work.
using System.Drawing.Printing;
...
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "The printer name"; // Load the appropriate printer's setting
After that, the various properties of PrinterSettings can be read.
Note that ps.isValid()
can see if the printer actually exists.
Edit: One additional comment. Microsoft recommends you use a PrintDocument and modify its PrinterSettings rather than creating a PrinterSettings directly.
Just for reference, here is a list of all the available properties for a printer ManagementObject.
usage: printer.Properties["PropName"].Value
Please notice that the article that dowski and Panos was reffering to (MSDN Win32_Printer) can be a little misleading.
I'm referring the first value of most of the arrays. some begins with 1 and some begins with 0. for example, "ExtendedPrinterStatus" first value in table is 1, therefore, your array should be something like this:
string[] arrExtendedPrinterStatus = {
"","Other", "Unknown", "Idle", "Printing", "Warming Up",
"Stopped Printing", "Offline", "Paused", "Error", "Busy",
"Not Available", "Waiting", "Processing", "Initialization",
"Power Save", "Pending Deletion", "I/O Active", "Manual Feed"
};
and on the other hand, "ErrorState" first value in table is 0, therefore, your array should be something like this:
string[] arrErrorState = {
"Unknown", "Other", "No Error", "Low Paper", "No Paper", "Low Toner",
"No Toner", "Door Open", "Jammed", "Offline", "Service Requested",
"Output Bin Full"
};
BTW, "PrinterState" is obsolete, but you can use "PrinterStatus".
It's been a long time since I've worked in a Windows environment, but I would suggest that you look at using WMI.
I know it's an old posting, but nowadays the easier/quicker option is to use the enhanced printing services offered by the WPF framework (usable by non-WPF apps).
http://msdn.microsoft.com/en-us/library/System.Printing(v=vs.110).aspx
An example to retrieve the status of the printer queue and first job..
var queue = new LocalPrintServer().GetPrintQueue("Printer Name");
var queueStatus = queue.QueueStatus;
var jobStatus = queue.GetPrintJobInfoCollection().FirstOrDefault().JobStatus
As an alternative to WMI you can get fast accurate results by tapping in to WinSpool.drv (i.e. Windows API) - you can get all the details on the interfaces, structs & constants from pinvoke.net, or I've put the code together at http://delradiesdev.blogspot.com/2012/02/accessing-printer-status-using-winspool.html
© 2022 - 2024 — McMap. All rights reserved.
IsDefaultPrinter
"Gets a value indicating whether the PrinterName property designates the default printer, except when the user explicitly sets PrinterName." – Bays