public string GetPrinter()
{
try
{
// USING WMI. (WINDOWS MANAGEMENT INSTRUMENTATION)
StringBuilder cmbPrinterList = new StringBuilder();
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)
{
if (Convert.ToBoolean(Printers["Local"])) // LOCAL PRINTERS.
{
cmbPrinterList.Append(Printers["Name"] + ",");
}
if (Convert.ToBoolean(Printers["Network"])) // ALL NETWORK PRINTERS.
{
cmbPrinterList.Append(Printers["Name"] + ",");
}
}
if (!string.IsNullOrEmpty(cmbPrinterList.ToString()))
return cmbPrinterList.ToString().Remove(cmbPrinterList.ToString().Length - 1);
else
return string.Empty;
}
catch (Exception ex)
{
EventLog e = new EventLog("Print Error");
e.WriteEntry("Failed in GetPrinter, Reason:" + ex.Message);
throw new FaultException(ex.ToString());
}
}
{
try
{
// USING WMI. (WINDOWS MANAGEMENT INSTRUMENTATION)
StringBuilder cmbPrinterList = new StringBuilder();
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)
{
if (Convert.ToBoolean(Printers["Local"])) // LOCAL PRINTERS.
{
cmbPrinterList.Append(Printers["Name"] + ",");
}
if (Convert.ToBoolean(Printers["Network"])) // ALL NETWORK PRINTERS.
{
cmbPrinterList.Append(Printers["Name"] + ",");
}
}
if (!string.IsNullOrEmpty(cmbPrinterList.ToString()))
return cmbPrinterList.ToString().Remove(cmbPrinterList.ToString().Length - 1);
else
return string.Empty;
}
catch (Exception ex)
{
EventLog e = new EventLog("Print Error");
e.WriteEntry("Failed in GetPrinter, Reason:" + ex.Message);
throw new FaultException(ex.ToString());
}
}
No comments:
Post a Comment