Skip to content

Instantly share code, notes, and snippets.

@unseensenpai
Created August 29, 2024 09:45
Show Gist options
  • Select an option

  • Save unseensenpai/e1f73fc611ed35d6663c228dccf1b679 to your computer and use it in GitHub Desktop.

Select an option

Save unseensenpai/e1f73fc611ed35d6663c228dccf1b679 to your computer and use it in GitHub Desktop.
WIA Device No Finder (TWAIN SOURCE)
namespace WIAHelper;
public class WIAHelper
{
readonly DeviceManager _deviceManager;
public WIAHelper()
{
_deviceManager = new DeviceManager();
}
private int FindDeviceNo(string deviceName, string devicePropertyName, WiaDeviceType? wiaDeviceType = null)
{
int deviceNo = 0;
int deviceCount = _deviceManager.DeviceInfos.Count;
for (int i = 1; i <= deviceCount; i++) // WIA Indexes Starts with 1
{
object index = i;
DeviceInfo deviceInfo = _deviceManager.DeviceInfos.get_Item(ref index);
foreach (Property item in deviceInfo.Properties)
{
object propertyItemValue = item.get_Value();
if (item.Name.ToString().Contains(devicePropertyName, StringComparison.OrdinalIgnoreCase) && (wiaDeviceType == null || deviceInfo.Type == wiaDeviceType))
{
object propertyNameValue = item.get_Value();
if (propertyNameValue is not null)
{
string propertyName = propertyNameValue.ToString();
if (propertyName.Contains(deviceName, StringComparison.OrdinalIgnoreCase))
{
deviceNo = i;
return deviceNo;
}
}
}
}
}
return deviceNo;
}
}
@unseensenpai
Copy link
Author

unseensenpai commented Aug 29, 2024

Usage:

FindDeviceNo(deviceName: "KODAK",  devicePropertyName: "NAME", wiaDeviceType: WiaDeviceType.ScannerDeviceType);
FindDeviceNo( deviceName: "S2060w",  devicePropertyName: "Description", wiaDeviceType: WiaDeviceType.ScannerDeviceType);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment