Naveen's Weblog

Bridge to future

Add, Delete and check if Exists WIN32_Printer

Posted by codingsense on February 6, 2009


To work with any WIN32 type like Win32 Logical disk or Win32 Process in .Net, we need to use System.Management.dll.

In system.Management.dll we can find 3 classes that are very important to handle management operations.

1) System.Management.ManagementScope – This class represents a scope for management operations.
2) System.Management.ManagementClass – This class represents a common Information Model (CIM) Management class.
3) System.Management.ManagementObject – Holds the object of the instance of management class.

Printer Operation

To explain about the Management Classes we will take an sample of Printer device. We shall see how to search a printer, install a printer and delete a printer in our system.

First we shall create a class MyPrinter and then fill it with required functions.

Create Printer Class:

public class Printer    
{
private ManagementScope managementScope = null;
//Add Below functions here    
}

Check if printer is Installed or Not:
Here we shall retrieve the printer information by executing a select query using the ManagementObjectSearcher. The search results can be retrived by using the Get() function, and this function returns a collection of printers maching the name. If the collection has any object then we return true or we return false.

public bool IsPrinterInstalled(string PrinterName)
{
managementScope = new ManagementScope(ManagementPath.DefaultPath); 
managementScope.Connect();
SelectQuery selectQuery = new SelectQuery();
selectQuery.QueryString = "Select * from win32_Printer where Name = '" + PrinterName + "'";
ManagementObjectSearcher MOS = new ManagementObjectSearcher(managementScope, selectQuery);
ManagementObjectCollection MOC = MOS.Get();
return MOC.Count > 0;
}

Add a new printer:
For adding a printer we will accept the needed parameter i.e Printer Name, Driver Name, Port No and is printer shared or not. After getting all the details we shall create a ManagementClass of type “Win32_Printer” Type and then create its instance. To this instance we add all the properties, and we add the new printer using the Put() method. Here in Put() function we can pass a class of type PutOptions. It contains many options that can be set, we set only the options needed for us ie PutType which will have the value UpdateOrCreate.

public bool AddPrinter(
string PrinterName, string PrinterDriver,
string PortName, bool SharedPrinter)
{
try            
{
//Create new Win32_Printer Class                
ManagementClass printerClass = new ManagementClass(managementScope, new ManagementPath("Win32_Printer"), null);

//create new Win32_Printer object                 
ManagementObject printerObject =
printerClass.CreateInstance();

//set port parameters                 
if (PortName == null || PortName.Length == 0)
printerObject["PortName"] = "LPT1:";
else                
{
if (PortName[PortName.Length - 1] != ':')
printerObject["PortName"] = (PortName + ":");
else
                        printerObject["PortName"] = PortName;
}
//set driver and device names 
                printerObject["DriverName"] = PrinterDriver;

printerObject["DeviceID"] = PrinterName;
//set sharing                 
if (SharedPrinter)
{
printerObject["Shared"] = SharedPrinter;
printerObject["ShareName"] = PrinterName;
}
// specify put options: update or create                 
PutOptions options = new PutOptions();
options.Type = PutType.UpdateOrCreate;

//put a newly created object to WMI objects set 
                printerObject.Put(options);

return true;
}
catch 
{
throw;
}
}

Delete a Printer:
Deleting a printer works in similar fashion like searching a printer, we need to search the printer then just add one more command to delete the printer if found

public bool DeletePrinter(string PrinterName)
{
managementScope = new ManagementScope(ManagementPath.DefaultPath); 
managementScope.Connect();
SelectQuery selectQuery = new SelectQuery();
selectQuery.QueryString = "Select * from win32_Printer where Name = '" + PrinterName + "'";
ManagementObjectSearcher MOS = new ManagementObjectSearcher(managementScope, selectQuery);
ManagementObjectCollection MOC = MOS.Get();
foreach (ManagementObject mo in MOC)
{
mo.Delete();
return true;
}
return false;
}

By adding all the three functions in the above created class , we will have ourself 3 functions that deals with Win32 Printer.

Happy Learning 🙂

Advertisements

2 Responses to “Add, Delete and check if Exists WIN32_Printer”

  1. Nathan said

    What is the point in this function if, according to Microsoft, it cannot be used remotely? I actually thought I was going to be able to create a tool for the helpdesk that creates new printer shares on one of our print servers.

  2. Vincent said

    How about setting default printer on a remote pc?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: