Hi guys, I'm an experienced VB programmer, but recently started using C# and I like it. But what I need to do now, is get a list of all printers installed on the computer (not a problem) and print a page to all of them (also not a problem)...
BUT (You knew there was a but coming...) it also tries to print to Virtual Printers (Like Adobe PDF, NoteWriter, XML loader, ect) which pops up a really annoying dialogue each time. I want to eventually use this automatically, IE no human intervention to close pesky dialogues.
So is there some way I can find out if it is a virtual printer?
Heres the relevant portion of my code:
String server = Console.ReadLine();
if (server.ToLower() == "y")
server = Environment.UserDomainName;
else
server = Environment.MachineName;
System.Management.ManagementObjectCollection moReturn = default(System.Management.ManagementObjectCollection);
System.Management.ManagementObjectSearcher moSearch = default(System.Management.ManagementObjectSearcher);
List<String> Printers = new List<String>();
try {
moSearch = new System.Management.ManagementObjectSearcher("Select * from Win32_Printer");
moSearch.Scope.Path.Server = server;
moReturn = moSearch.Get();
Console.WriteLine("Fetching Printer list from " + server + "...");
foreach (System.Management.ManagementObject mo in moReturn) {
Printers.Add(mo.GetPropertyValue("name").ToString());
Console.WriteLine(mo.GetPropertyValue("name"));
}
} catch (Exception ex) {
Console.WriteLine(ex.Message);
} finally {
moReturn = null;
moSearch = null;
}
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);
foreach (String printer in Printers)
{
Pos = 0;
pd.PrinterSettings.PrinterName = printer;
PB = new Rectangle(0, 0, pd.DefaultPageSettings.Bounds.Width, pd.DefaultPageSettings.Bounds.Height);
try
{
pd.Print();
}
catch
{
continue;
}
}