I have a windows forms application where I am trying to print something but if there is no actual printer (not xps or something else) I want to show a message box which indicates that there is no actual printer. So in short I want them to print if there is a real printer set as default in the current computer which leads me to check if the default printer is real.
Asked
Active
Viewed 331 times
0
-
Check [this link](http://social.msdn.microsoft.com/Forums/vstudio/en-US/a4de14b8-8d59-467b-a4b1-971dd340c72c/physical-device-printer-or-document-writer-printer) out – Icemanind May 24 '14 at 07:55
1 Answers
2
You can do this using System.Management. Use the following class to solve your problem:
public class MyClass
{
static void printProps(ManagementObject o, string prop)
{
try
{
Console.WriteLine(prop + "|" + o[prop]);
}
catch (Exception e)
{
Console.Write(e.ToString());
}
}
[STAThread]
static void Main(string[] args)
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_Printer where Default=True");
string printerName = "";
foreach (ManagementObject printer in searcher.Get())
{
printerName = printer["Name"].ToString().ToLower();
Console.WriteLine("Printer :" + printerName);
printProps(printer, "WorkOffline");
//Console.WriteLine();
switch (Int32.Parse(printer["PrinterStatus"].ToString()))
{
case 1: Console.WriteLine("Other"); break;
case 2: Console.WriteLine("Unknown"); break;
case 3: Console.WriteLine("Idle"); break;
case 4: Console.WriteLine("Printing"); break;
case 5: Console.WriteLine("Warmup"); break;
case 6: Console.WriteLine("Stopped printing"); break;
case 7: Console.WriteLine("Offline"); break;
}
}
}
}

Syed Farjad Zia Zaidi
- 3,302
- 4
- 27
- 50
-
Yea I've been there, but the thing is he uses his own name of the printer which he knows the printer. In my case we dont know the name of the printer. – MCanSener May 24 '14 at 07:44
-
using WMI is the way to go, but I don't think you can tell if it's a 'real printer'. a printer driver is just that, an output stream. it has no idea what device is connected to it at the hardware level. you'd have to rule out all 'non-real' devices, as you perceive them to be, or white-list all 'real' devices by name. although you might be able to rule some out based on the PortName as defined in the WMI properties returned for that printer. http://stackoverflow.com/questions/296182/how-to-get-printer-info-in-net – porkchop May 24 '14 at 08:39