how to get printer information like printer name,IP and location using c-sharp??
skinder 0 Newbie Poster
vibhutirs 0 Newbie Poster
sanch01r 0 Light Poster
Skinder, you can use WMI to query printer information (Win32_Printer). Below is some sample code that I wrote a while ago, this will retrieve every printer mapped to your computer and give you all available details about it. Create a windows form application and add a Button and TreeView and paste the code below. I just tested it out and it works, let me know if you have any problems. Good luck and hope this helps.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace PrinterQuery
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
TreeNode ParentNode, Node;
ManagementClass Class = new ManagementClass("\\\\127.0.0.1\\root\\CIMV2:Win32_Printer");
Class.Options.UseAmendedQualifiers = true;
Class.Options.Timeout = new TimeSpan(0, 0, 0, 3, 0);
PropertyDataCollection Properties = Class.Properties;
if (Class.Properties.Count > 0)
{
foreach (ManagementObject Result in Class.GetInstances())
{
ParentNode = WMIPropertyTV.Nodes.Add(Result.Properties["Name"].Value.ToString());
foreach (PropertyData Property in Result.Properties)
{
Node = ParentNode.Nodes.Add(Property.Name);
if (Result.GetPropertyValue(Property.Name) != null)
{
if (Result.GetPropertyValue(Property.Name).GetType().IsArray)
AddADTreeArrayValues(Node, new ResultProperty(Result.GetPropertyValue(Property.Name)));
else Node.Nodes.Add(GetMOString(new ResultProperty(Property.Name), Result));
}
else Node.Nodes.Add("Null");
}
}
}
}
private string GetMOString(ResultProperty Property, ManagementObject Result)
{
return Result.GetPropertyValue(Property.String) != null ? Result.GetPropertyValue(Property.String).ToString() : "Null";
}
private void AddADTreeArrayValues(TreeNode ParentNode, ResultProperty Property) //Some WMI attributes are returned as arrays.
{
TreeNode ArrayIDNode;
if (Property.Type == typeof(string[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.String[" + Property.StringArray.Length.ToString() + "]");
foreach (string s in Property.StringArray)
if (s.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(s));
else ArrayIDNode.Nodes.Add(s);
}
else if (Property.Type == typeof(byte[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.Byte[" + Property.ByteArray.Length.ToString() + "]");
foreach (byte b in Property.ByteArray)
if (b.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(b));
else ArrayIDNode.Nodes.Add(b.ToString());
}
else if (Property.Type == typeof(int[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.Int[" + Property.INTArray.Length.ToString() + "]");
foreach (int i in Property.INTArray)
if (i.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(i));
else ArrayIDNode.Nodes.Add(i.ToString());
}
else if (Property.Type == typeof(ushort[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.UInt16[" + Property.UShortArray.Length.ToString() + "]");
foreach (ushort u in Property.UShortArray)
if (u.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(u));
else ArrayIDNode.Nodes.Add(u.ToString());
}
else if (Property.Type == typeof(object[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.Object[" + Property.ObjectArray.Length.ToString() + "]");
foreach (object o in Property.ObjectArray)
if (o.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(o));
else ArrayIDNode.Nodes.Add(o.ToString());
}
}
}
struct ResultProperty
{
object _Value;
bool _IsArray;
string[] _StringArray;
object[] _ObjectArray;
byte[] _ByteArray;
int[] _IntArray;
ushort[] _UShortArray;
Type _T;
public ResultProperty(object Value)
{
_Value = Value;
_IsArray = Value.GetType().IsArray;
_T = Value.GetType();
_StringArray = null;
_ObjectArray = null;
_ByteArray = null;
_IntArray = null;
_UShortArray = null;
}
public string String { get { return GetString(); } }
public int INT { get { return GetInt(); } }
public Int64 INT64 { get { return GetInt64(); } }
public DateTime Date { get { return GetDate(); } }
public object Value { get { return _Value; } }
public bool IsArray { get { return _IsArray; } }
public string[] StringArray { get { return ToStringArray(); } }
public byte[] ByteArray { get { return ToByteArray(); } }
public int[] INTArray { get { return ToIntArray(); } }
public ushort[] UShortArray { get { return ToUShortArray(); } }
public object[] ObjectArray { get { return ToObjectArray(); } }
public Type Type { get { return GetTypeOf(); } }
private Type GetTypeOf()
{
try { return _T; }
catch { throw new Exception("Unable to retrieve T type"); }
}
private string GetString()
{
try { return Convert.ToString(_Value); }
catch { return null; }
}
private DateTime GetDate()
{
try { return Convert.ToDateTime(_Value); }
catch { return new DateTime(); }
}
private Int64 GetInt64()
{
try { return Convert.ToInt64(_Value); }
catch { return Int64.MinValue; }
}
private int GetInt()
{
try { return Convert.ToInt32(_Value); }
catch { return int.MinValue; }
}
private int GetInt(object value)
{
try { return Convert.ToInt32(value); }
catch { return int.MinValue; }
}
private UInt16 GetUInt16()
{
try { return Convert.ToUInt16(_Value); }
catch { return ushort.MinValue; }
}
private ushort GetUInt16(object value)
{
try { return Convert.ToUInt16(value); }
catch { return ushort.MinValue; }
}
private byte[] ToByteArray()
{
if (!(_IsArray && Value.GetType() == typeof(byte[]))) return null;
try
{
byte[] barray = (byte[])Value;
List<byte> bytearray = new List<byte>();
foreach (byte b in barray) bytearray.Add(b);
return _ByteArray = bytearray.ToArray();
}
catch
{
return null;
}
}
private int[] ToIntArray()
{
if (!_IsArray) return null;
try
{
int[] iarray = (int[])Value;
List<int> intarray = new List<int>();
foreach (int i in iarray) intarray.Add(GetInt(i));
return _IntArray = intarray.ToArray();
}
catch
{
return null;
}
}
private object[] ToObjectArray()
{
if (!_IsArray) return new object[] { _Value };
try
{
object[] objectarray = (object[])_Value;
List<object> oarray = new List<object>();
foreach (object o in objectarray) oarray.Add(o != null ? o.ToString() : "");
return _ObjectArray = oarray.ToArray();
}
catch
{
return null;
}
}
private ushort[] ToUShortArray()
{
if (!_IsArray) return null;
try
{
ushort[] UShortArray = (ushort[])Value;
List<ushort> uarray = new List<ushort>();
foreach (ushort u in UShortArray) uarray.Add(GetUInt16(u));
return _UShortArray = uarray.ToArray();
}
catch
{
return null;
}
}
private string[] ToStringArray()
{
if (_StringArray != null) return _StringArray;
if (!(_IsArray && (Value.GetType() == typeof(string[]) || Value.GetType() == typeof(object[])))) return new string[] { Value.ToString() };
try
{
object[] oarray = (object[])Value;
List<string> outarray = new List<string>();
foreach (object obj in oarray) outarray.Add(obj != null ? obj.ToString() : "");
return _StringArray = outarray.ToArray();
}
catch
{
return null;
}
}
}
}
Edited by sanch01r because: n/a
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.