C# Wmi Install Printer Driver

In today s programming tutorial, I am going to describe some basics about how we can perform serial port communication from our C. NET applications.

This is another part of my series regarding start up scripts. This post is for Printer Management, now why do I have to do that. I was tasked to do this as we have to map each printer in every shop we had when a user logs in. Now this is all managed in a SQL Server database where their subnets which is unique per shop are mapped to a printer or many printers, so any configuration changes have to be done in SQL Server rather than manually updating all of the clients, these configuration changes are things such as renaming printers we have to do this as we still have some legacy apps that rely on Printer1 and Printer2, if you know what I mean, changing the IP Ports, changing default printers and much more.

Now I had created a class library to handle the Printer Management, I have 6 main methods that I normally use:

AddPrinter – Adds or maps a specific printer to the client machine

DeletePrinter - Removes or unmaps a specific printer to the client machine

RenamePrinter – Renames the printer on the client machine

SetDefaultPrinter – Sets the printer as default on clients machine

GetPrinterInfo – Gets the Printer Information

IsPrinterInstalled – Checks whether the printer is installed

Now for the codes:

using System;

using System.Management;

class PrinterSettings

private static ManagementScope oManagementScope null;

public static bool AddPrinter string sPrinterName

try

oManagementScope new ManagementScope ManagementPath.DefaultPath ;

oManagementScope.Connect ;

ManagementClass oPrinterClass new ManagementClass

new ManagementPath Win32_Printer, null ;

ManagementBaseObject oInputParameters

oPrinterClass.GetMethodParameters AddPrinterConnection ;

oInputParameters.SetPropertyValue Name, sPrinterName ;

oPrinterClass.InvokeMethod AddPrinterConnection, oInputParameters, null ;

return true;

catch Exception ex

return false;

public static bool DeletePrinter string sPrinterName

SelectQuery oSelectQuery new SelectQuery ;

oSelectQuery.QueryString SELECT FROM Win32_Printer WHERE Name

sPrinterName.Replace, ;

ManagementObjectSearcher oObjectSearcher

new ManagementObjectSearcher oManagementScope, oSelectQuery ;

ManagementObjectCollection oObjectCollection oObjectSearcher.Get ;

if oObjectCollection.Count. 0

foreach ManagementObject oItem in oObjectCollection

oItem.Delete ;

//Renames the printer

public static void RenamePrinter string sPrinterName, string newName

oSelectQuery.QueryString SELECT FROM Win32_Printer

WHERE Name sPrinterName.Replace, ;

oItem.InvokeMethod RenamePrinter, new object newName ;

return;

//Sets the printer as Default

public static void SetDefaultPrinter string sPrinterName

oItem.InvokeMethod SetDefaultPrinter, new object sPrinterName ;

//Gets the printer information

public static void GetPrinterInfo string sPrinterName

Console.WriteLine Name : oItem Name. ToString ;

Console.WriteLine PortName : oItem PortName. ToString ;

Console.WriteLine DriverName : oItem DriverName. ToString ;

Console.WriteLine DeviceID : oItem DeviceID. ToString ;

Console.WriteLine Shared : oItem Shared. ToString ;

Console.WriteLine --------------------------------------------------------------- ;

//Checks whether a printer is installed

public bool IsPrinterInstalled string sPrinterName

return oObjectCollection.Count 0;

If you had noticed, on my WMI Queries I added on each query you need this because it terminates the character , to make it more confusing my Printer Name variable is something similar to this ServerName PrinterName. To make it more confusing I added Replace, . Now what the hell am I doing.

The issue is that the character is the escape symbol for both C and WQL and Using tells C not to escape the but you need to tell WQL the same thing. In C, I had used to escape the querystring which instructs the compiler to treat ANY special characters in the string as literals which then means you don t need to escape the symbol for C but we need an extra as an escape for WQL parser.

To further elaborate, in my parameter sPrinterName the value as text is ServerName PrinterName but since it s in C you will see when you put a watch on it, it will show as ServerName PrinterName it s only when it is passed as the parameter then it is recognized as ServerName PrinterName that s why I have to do the replace so it would be ServerName PrinterName again which is a proper format for WQL and add for C to escape it.

In this second part of the series, you will learn how to work with WMI classes and class instances, and then demonstrates the wealth of information available through.

Language:

English

The WMI Code Creator tool allows you to generate VBScript, C, and VB. NET code that uses WMI to complete a management task such as querying for management data, executing a method from a WMI class, or receiving event notifications using WMI.

Details

The WMI Code Creator tool generates code that uses WMI to obtain management information or perform management tasks. You can use the tool to learn how to manage computers using WMI scripting and WMI. NET. The tool generates code that runs on the local computer, a remote computer, or a group of remote computers based on your selection from the Target Computer menu on the tool. You can also execute the generated code directly from the tool.

The tool is meant to help IT Professionals quickly create management scripts and to help developers learn WMI scripting and WMI. NET. The tool helps take the complexity out of writing code that uses WMI and helps developers and IT Professionals understand how powerful and useful WMI can be for managing computers.

Using the tool, you can query for management information such as the name and version of an operating system, how much free disk space is on a hard drive, or the state of a service. You can also use the tool to execute a method from a WMI class to perform a management task. For example, you can create code that executes the Create method of the Win32_Process class to create a new process such as Notepad or another executable. The tool also allows you to generate code to receive event notifications using WMI. For example, you can select to receive an event every time a process is started or stopped, or when a computer shuts down.

The tool also allows you to browse through the available WMI namespaces and classes on the local computer to find their descriptions, properties, methods, and qualifiers.

The code that creates the tool is also included in the download. The tool was created using WMI. NET, and the code for the tool can help developers understand how WMI. NET is used to create applications and manage information. Be sure to read the end-user license agreement that is included in the download.

System Requirements

Supported Operating System

Windows 2000, Windows 2000 Advanced Server, Windows 2000 Professional Edition, Windows 2000 Server, Windows 2000 Service Pack 2, Windows 2000 Service Pack 3, Windows 2000 Service Pack 4, Windows Server 2003, Windows Server 2003 Service Pack 1, Windows XP, Windows XP 64-bit, Windows XP Home Edition, Windows XP Professional Edition, Windows XP Service Pack 1, Windows XP Service Pack 2

. NET Framework 1.1 or later

Install Instructions

After downloading the. zip file, extract the contents from the. zip file into a folder of your choice. Read the end-user license agreement EULA_WMI_CODE_CREATOR.rtf and then double-click the WMICodeCreator.exe file to start the tool. There is a help menu on the tool to help you navigate the user interface of the tool.

Related Resources

Popular downloads

Product.

WMI Code Creator v1.0 Managing Printers Programatically using C# and WMI

The START command. Start a program in a new window or session. Both OS/2 and Windows 32 bits feature a START command to start a program in a new, separate.

Late, but maybe can help if you are using. inf file. To do this without any wizard, the driver must be signed. Windows 7: INSTALLATION: First Command install.

UberAgent. Close; Get started. Benefits and Use Cases; Customer Success Stories; Videos; uberAgent for XenApp XenDesktop; Install and Configure Splunk; Install.

The WMI Code Creator tool allows you to generate VBScript, C, and VB. NET code that uses WMI to complete a management task such as querying for management.

WMI Windows Management Instrumentation is a component of the Microsoft operating system that allows you to monitor virtually every piece of the system either.