开发者

How to change a printer's PortName using c#

开发者 https://www.devze.com 2023-03-31 07:45 出处:网络
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\"

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开发者_如何学Python build a port. (BTW I know the port works because setting it in the printer properties works fine).

Your help appreciated.

Thanks


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号