C# PrintDocument and Printer Status - c#

I am trying to get the printer status of a PointOfSale printer using the following code:
Hashtable properties = new Hashtable();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win2_Printer");
foreach (ManagementObject obj in searcher.Get()) {
if (obj["name"].ToString() == printerName) {
foreach (PropertyData data in obj.Properties) {
if(data.Name.ToLower() = "printerstatus") {
int printerStatus = Convert.ToInt32(data.Value);
}
}
}
}
Problem is, the status is either 3 (idle) or 4(printing), even when unplugged or the paper is out.
I have read a lot of posts with this same issue, but have not found an answer. Is this correct? How else would I check the status? Any help is appreciated.

What Brand of printer are you using?
Sometimes the Brand will have a specific command you can send to query the status.

Related

How can I find out if my microphone is muted

As the title says I'd like to check if my microphone is muted. Or even better get an event if it gets muted/unmuted.
I tried to get some information form the internet. Turns out I simply don't get it. I found out about the "new" Audio Core API which gave me some ideas, sadly I did not find any c# code so I cant figure out how to use any of this.
With the following code I could get some information for my microphone, however nothing changes if I mute it.
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_SoundDevice");
ManagementObjectCollection objCollection = objSearcher.Get();
ManagementObject rodeMic = null;
foreach (ManagementObject obj in objCollection)
{
foreach (PropertyData property in obj.Properties)
{
if (property.Name == "DeviceID" && (string) property.Value == "USB\\VID_...")
{
rodeMic = obj;
}
}
}
if (rodeMic != null)
{
foreach (var property in rodeMic.Properties)
{
Console.Out.WriteLine(String.Format("{0}:{1}", property.Name, property.Value));
}
}

How to contentiously loop through in order to get printer Status

I have coded a simple console application that checks the status of a printer. When status of a printer changes to a "printing status" the console app simply writes out a message saying "The Printer is now Printing".
Now what i'm having difficulties with is making this program keep checking the status of printer .. I'm not so sure what loop i have to use and how i can apply it. Please see below for more information:
public static void getPrintJob()
{
string printerName = "Some Printer Name";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();
foreach (ManagementObject printer in coll)
{
if (Convert.ToInt32(printer.Properties["PrinterStatus"].Value) == 4)
{
Console.Write("Printer is Printing");
}
What kind of loop could i put above for this program to continuously keep on checking the the printer status? and whenever the status changes to 4 (printing status on the printer i'm targeting ).
If you want this to run forever, a while loop will work:
public static void getPrintJob()
{
string printerName = "Some Printer Name";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
while(true)
{
ManagementObjectCollection coll = searcher.Get();
var alreadyPrinting = false;
foreach (ManagementObject printer in coll)
{
if (Convert.ToInt32(printer.Properties["PrinterStatus"].Value) == 4 && !alreadyPrinting)
{
Console.Write("Printer is Printing");
alreadyPrinting = true;
}
else
{
alreadyPrinting = false;
}
}
Thread.Sleep(1000);
}
}
As RB pointed out in the comments, Thread.Sleep(1000) will pause the loop for 1 second and stop it consuming all the CPU resource.
EDIT:
updated after comments.
Added a bool to track if the printer was already printing. This stops the code writing to the console for as long as the printer is printing. Multiple printers can start and will write to the console. But only once per print job.
Moved the coll variable assignment inside the while loop.

C# ManagementObjectSearcher object container returns null for printers

I have a strange issue, I hope somebody can help me out. A software was deployed without any problem 2 months ago. Now I got back to it, and it returns an invalid class error for the following code. All what it does, it returns all the printers installed on the computer.
The error is, the searcher.Container = null. In the past it was list of printer objects. Nothing has changed since then, the computer is a 64 bit Windows 7, using Visual Studio 2015.
public static ObservableCollection<PrinterStatusData> GetAvailablePrinterAndStatus()
{
ObservableCollection<PrinterStatusData> printerStatus = new ObservableCollection<PrinterStatusData>();
ManagementScope scope = new ManagementScope(#"\root\cimv2");
scope.Connect();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
foreach (ManagementBaseObject o in searcher.Get())
{
ManagementObject printer = (ManagementObject)o;
if (printer != null)
{
string name = printer["Name"].ToString();
bool available = !printer["WorkOffline"].ToString().ToLower().Equals("true");
printerStatus.Add(new PrinterStatusData(name, available));
}
}
return printerStatus;
}
if (printer["Name"] != null)
{
string name = printer["Name"].ToString();
bool available = !printer["WorkOffline"].ToString().ToLower().Equals("true");
printerStatus.Add(new PrinterStatusData(name, available));
}

Retrieve a List of Printers on the Network Using WMI, C#

I've developed a printer search application that search for all online printers on the network. Im referring to this site - here
The button event handler is something like:
private void btnGetPrinters_Click(object sender, EventArgs e)
{
// Use the ObjectQuery to get the list of configured printers
System.Management.ObjectQuery oquery =
new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
System.Management.ManagementObjectSearcher mosearcher =
new System.Management.ManagementObjectSearcher(oquery);
System.Management.ManagementObjectCollection moc = mosearcher.Get();
foreach (ManagementObject mo in moc)
{
System.Management.PropertyDataCollection pdc = mo.Properties;
foreach (System.Management.PropertyData pd in pdc)
{
if ((bool)mo["Network"])
{
cmbPrinters.Items.Add(mo[pd.Name]);
}
}
}
}
but it's not working. I've debugged by line an found that the ((bool)mo["Network"]) cond. return false. Anyone have idea why this happen? I've checked my network and printers conn. It's all working fine. please advice.

Find available data in an ManagementObject

I'm using ManagementObjectSearcher to load all the printers available in the network. There all the printers are returned in a ManagementObjectCollection. Is there anyway to find out all the details returned?
I used the debugging of c# to preview the object but it does not show all the data in there. I want to know what is available other than Printers[Name],Printers[Local],Printers[Network]. Is there a possible way to do this?
Code
System.Management.ManagementScope objMS =
new System.Management.ManagementScope(ManagementPath.DefaultPath);
objMS.Connect();
SelectQuery objQuery = new SelectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher objMOS = new ManagementObjectSearcher(objMS, objQuery);
System.Management.ManagementObjectCollection objMOC = objMOS.Get();
foreach (ManagementObject Printers in objMOC)
{
System.Management.PropertyDataCollection pdc = Printers.Properties;
if (Convert.ToBoolean(Printers["Local"])) // LOCAL PRINTERS.
{
comboBox8.Items.Add(Printers["Name"]);
}
if (Convert.ToBoolean(Printers["Network"])) // ALL NETWORK PRINTERS.
{
comboBox9.Items.Add(Printers["Name"]);
}
}
Generate strongly typed classes for Win32_Printer that will show you everything you need.
http://msdn.microsoft.com/en-us/library/2wkebaxa(v=vs.110).aspx - Mgmtclassgen.exe (Management Strongly Typed Class Generator)
You can enumerate the ManagementBaseObject.Properties property, and PropertyData.Qualifiers.
foreach (PropertyData property in properties)
{
Console.WriteLine(property.Name);
foreach (QualifierData q in property.Qualifiers)
{
if(q.Name.Equals("Description"))
{
Console.WriteLine(
processClass.GetPropertyQualifierValue(
property.Name, q.Name));
}
}
Console.WriteLine();
}
From MSDN

Categories

Resources