Introduction
This is a small utility to set the default printer of the local system based on the network that the system is connected to.
This utility gets the printer-network mapping from an XML file. This enables the user to modify the printer-network mapping.
This utility uses Windows Management Instruments (WMI) to access system printers information and set the default printer based on the user selection.
To know more about Windows Management Instruments, check this link.
System.Management
namespace provides a powerful class to access various types of system information.
Limitation of WMI
WMI does not work properly with Windows 2000. It works fine with Windows XP Service Pack 2 or Windows Vista. For more information, check this Web site.
There is another problem that I faced while developing this utility.
Debugging the code. Enumerating through a ManagementObjectCollection
is not possible.
In addition, the properties of a ManagementObject
are unknown. I had to get through the Internet to find proper documentation. But I didn't find one.
How It Works
To get a particular device or system information, ManagementObjectSearcher
or ManagementQuery
is used.
string query = String.Format("SELECT * FROM Win32_NetworkAdapterConfiguration"
+ " WHERE MACAddress = '{0}'", macAddress.ToString());
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();
if (moCollection.Count == 0)
throw new Exception(String.Format("The NIC Card ({0})
information not found in windows.", macAddress.ToString()));
List<networkadapterconfiguration /> config = new List<networkadapterconfiguration />();
foreach (ManagementObject mo in moCollection)
{
string[] addresses = (string[])mo["IPAddress"];
if (addresses == null) continue;
if (addresses[0] != ni.GetIPProperties().UnicastAddresses[0].Address.ToString())
continue;
string[] subnets = (string[])mo["IPSubnet"];
}
You can also access system resource information if you know the full path of the resource as follows:
String printerPath="win32_printer.DeviceId='" + printerName + "'";
ManagementObject printer = new ManagementObject(printerPath);
Using ManagementObjectSearcher
:
ManagementScope scope = new ManagementScope(@"\root\cimv2");
pscope.Connect();
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(QUERY_ALLPRINTER);
return searcher.Get();
Setting the Default Printer
To set the default printer:
ManagementBaseObject outParams;
try
{
outParams = printer.InvokeMethod("SetDefaultPrinter", null, null);
if (outParams == null)
throw new Exception("Unable to set default printer.");
Int32 retVal = (int)(uint)outParams.Properties["ReturnValue"].Value;
if (retVal == 0)
return true;
else
return false;
}
catch (Exception ex)
{
throw ex;
}
Network Address Calculation
To get the Network Address (Network ID) of the system, the following code is used:
public static String CalculateNetworkAddress(String ip, String mask)
{
IPAddress _ip;
IPAddress _subnet;
if (!IPAddress.TryParse(ip, out _ip))
throw new FormatException
(PrinterUtility.Properties.Resources.IPAddressException);
if (!IPAddress.TryParse(mask, out _subnet))
throw new FormatException
(PrinterUtility.Properties.Resources.SubnetMaskException);
StringBuilder networkAddress = new StringBuilder("");
for (int index = 0; index < 4; index++)
{
int networkAddressBlock = _ip.GetAddressBytes()[index] &
_subnet.GetAddressBytes()[index];
networkAddress.Append(networkAddressBlock.ToString());
networkAddress.Append(".");
}
return networkAddress.ToString().TrimEnd('.'); ;
}
Before that, you have to get the system IP Address and Subnet Mask. To do that, you can use NetworkInterface
class under System.Net
:
NetworkInterface[] nis =NetworkInterface.GetAllNetworkInterfaces();
History
- 27th May, 2008: Initial post