1-) C# RMOS - bilgisayarda kurulu ağda bulunan tüm yazıcı isimlerini iplerini vs alma How to get all network printers
1. Adım YaziciModel.cs
public class YaziciModel
{
public string Name { get; set; }
public string Ip { get; set; }
public string Port { get; set; }
public string DriverName { get; set; }
public bool WorkOffline { get; set; }
public object PrinterState { get; set; }
public bool isActive { get; set; }
}
2. Adım Ping ile Yazıcı Aktifmi metodu
public static bool PingHost(string nameOrAddress)
{
if (nameOrAddress == null) return false;
bool pingable = false;
Ping pinger = null;
try
{
pinger = new Ping();
PingReply reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException)
{
// Discard PingExceptions and return false;
}
finally
{
if (pinger != null)
{
pinger.Dispose();
}
}
return pingable;
}
3. Adım Kullanılan Metot
[HttpGet]
public object getAllPrintNameDetail(bool isActive=false)
{
try
{
List<YaziciModel> yaziciList = new List<YaziciModel>();
var scope = new ManagementScope(@"\root\cimv2");
scope.Connect();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
var results = searcher.Get();
foreach (var printer in results)
{
YaziciModel yaziciModel = new YaziciModel();
var portName = printer.Properties["PortName"].Value;
var searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + portName + "'");
var results2 = searcher2.Get();
string isim = printer["Name"].ToString();
foreach (var printer2 in results2)
{
var namee = printer.Properties["Name"].Value;
yaziciModel.Name = namee.ToString();
yaziciModel.Port = printer2.Properties["PortNumber"].Value.ToString();
yaziciModel.Ip = printer2.Properties["HostAddress"].Value.ToString();
}
if (yaziciModel.Name == null || yaziciModel.Name == "null")
{
yaziciModel.Name = isim;
}
yaziciModel.DriverName = printer["DriverName"].ToString();
yaziciModel.WorkOffline = (bool)printer["WorkOffline"];
yaziciModel.PrinterState = printer["PrinterState"];
if (isActive) // hızlı sorgulasın diye bu parametriktir
{
yaziciModel.isActive = PingHost(yaziciModel.Ip);
}
//var server = new LocalPrintServer();
//PrintQueue queue = server.GetPrintQueue(yaziciModel.Name, Array.Empty<string>());
yaziciList.Add(yaziciModel);
}
return yaziciList;
}
catch (Exception ex)
{
return "hata 56 " + ex.Message;
}
}
***** KAYNAKLAR ve Kütüphaneler *****
using System;
using System.Collections.Generic;
using System.IO;
using System.Management;
using System.Net.NetworkInformation;
using System.Web.Http;
4. DİĞER SADECE İSİM ALMAK İÇİN
DataTable table = new DataTable(); table.Columns.Add("Pkod_Ad", typeof(string)); foreach (var item in PrinterSettings.InstalledPrinters) { DataRow dataRow = table.NewRow(); dataRow["Pkod_Ad"] = item; table.Rows.Add(dataRow); }
|