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?
This question is related to
c#
.net
printing
printers
printdialog
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.
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
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
It's been a long time since I've worked in a Windows environment, but I would suggest that you look at using WMI.
It's been a long time since I've worked in a Windows environment, but I would suggest that you look at using WMI.
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
It's been a long time since I've worked in a Windows environment, but I would suggest that you look at using WMI.
Just for reference, here is a list of all the available properties for a printer ManagementObject.
usage: printer.Properties["PropName"].Value
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
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".
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.
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".
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.
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
Source: Stackoverflow.com