Reputation: 5798
I have HP Scanner Scanjet 5590 attached to my system. I want to know the status of the scanner via c#.net. How to do it? The status are :
on/off, idle, scanning, etc..
Update: Also I have tried using WMI but it is not detecting it. In the control panel of Win-7 the scanner is shown under the head DEVICES and not under the head PRINTERS.
Kindly help.
Upvotes: 1
Views: 2945
Reputation: 12884
string printerName = "HP Scanner Scanjet 5590";
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)
{
foreach (PropertyData property in printer.Properties)
{
Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
}
}
Upvotes: 0
Reputation: 8962
You could try to use the Windows Image Acquisition (WIA) API. WIA 2.0 was released with Windows Vista and is mainly targeted towards scanners. It is still supported for Windows 7. I tested it with a HP Scanjet 4670 scanner a while back. Your scanner should be compatible with the WIA API.
To access WIA 2.0, you’ll need to add a reference to the COM library “Microsoft Windows Image Acquisition Library v2.0″.
Once you have added the reference you can enumerate the WIA compatible devices.
var deviceManager = new DeviceManager();
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
var deviceName =
deviceManager.DeviceInfos[i].Properties["Name"].get_Value().ToString();
// Is the device a scanner?
if (deviceManager.DeviceInfos[i].Type == WiaDeviceType.ScannerDeviceType)
{
//...etc.
}
}
Remark: Be sure to treat the DeviceInfos array as a 1-baed array instead of a zero-based array! You'll get COM exceptions if you don't.
When you find your scanner in the DeviceInfos[...] array you can connect to it.
DeviceInfo deviceInfo = deviceManager.DeviceInfos[1];
deviceInfo.Connect();
Once connected you can operate it. Let's scan an image.
// Start the scan
var item = deviceInfo.Items[1];
var imageFile = (ImageFile) item.Transfer(FormatID.wiaFormatJPEG);
You can find more information on the above here:
Windows Image Acquisition (WIA)
Using the WIA API you are at least able to detect if the scanner is attached to your system and is powered on. That deals with the on/off issue.
You can also use WIA to query the device properties.
Scanner Device Property Constants
The following device property might interest you:
WIA_DPS_DOCUMENT_HANDLING_STATUS: Contains current state of the scanner's installed flatbed, document feeder, or duplexer (ready, paper jam, lamp error...etc.).
Query the WIA_DPS_DOCUMENT_HANDLING_STATUS to check the current status of the scanner.
For example:
class WIA_PROPERTIES
{
public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
public const uint WIA_DIP_FIRST = 2;
public const uint WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
// Scanner only device properties
public const uint WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
}
Property documentHandlingStatus = null;
foreach (Property property in device.Properties)
{
string propertyName = property.Name;
string propertyValue = property.get_Value().ToString();
if (property.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
{
// ...
}
}
Checkout Microsoft's WiaDef.h header file for the values of these device property constants.
Upvotes: 2
Reputation: 362
u can use WMI to get information like this. See this post How to get Printer Info in .NET? as a reference.
Update: Try to search for PnP entities:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_PnPEntity");
ManagementObjectCollection coll = searcher.Get();
foreach (ManagementObject any in coll)
{
// Check for device name
}
This should give you a list of all devices.
Upvotes: 4