3

I've scanned SO and didn't see this question posted, sorry if I missed it and this is a repeat.

I can locate the network printer in the PrinterSettings.InstalledPrinters, and fetch the "PortName" property from printer.Properties, but just setting the property doesn't work. I tried to brute force the change for that printer portname in the registry and that didn't work either (although I really didn't think it would but had to try).

I have the new port implemented through a reg file load so don't need to build a port. (BTW I know the port works because setting it in the printer properties works fine).

Your help appreciated.

Thanks

ddm
  • 478
  • 5
  • 14

1 Answers1

3

You could use WMI to set the PortName for your printer. Here is an example:

ManagementScope scope = new ManagementScope(@"\root\cimv2");
scope.Connect();

// Insert your printer name in the WHERE clause...
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer WHERE Name='PrinterName");


foreach (ManagementObject printer in searcher.Get())
{
  printer["PortName"]="LPT1:";
  printer.Put();  // Important: Call put to save the settings.
}

Hope, this helps.

Hans
  • 12,902
  • 2
  • 57
  • 60
  • 1
    I'll assume this works so thanks Man - I really appreciate this. ManagementScope. Interesting. I need to research this more. – ddm Aug 29 '11 at 20:52