[VB] Change printer port via reg

Discussion in 'Mixed Languages' started by stevemk14ebr, May 12, 2012.

  1. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #1 stevemk14ebr, May 12, 2012
    Last edited by a moderator: Apr 20, 2017
    alright so i've written a pretty simple program that will change the reg value for a printer via reg then restart the spooler service to apply that change. the problem is that this doesn't actually change the port correctly. When printing a document after the port change the small printer icon in the tray displays the document as corretly printing to the just changed to port, the registry shows the value is correct, and the spooler service correctly reboots after the reg change, but it never actually prints.My question is, is there a step that i missed to change the printer port (the non code way is to go into control panel>devices and printers>right click on the default printer and click properties>go to the port tab>click add new port>create standard tcp/ip port> enter ip of printer>windows will search, find the printer and display irrelivent info, and i click next and apply and exit and port is changed, then the document prints)

    My current (non working way) of doing this code wise is to just change the reg value of port (same as ip of printer) and restart spooler, code:
    Code:
    Imports System.ServiceProcess
    Public Class Form1
        Dim printerip As String
        Private Sub btn_Click(sender As System.Object, e As System.EventArgs) Handles btn.Click
            Try
                printerip = IP.Text
                Dim autoshell = My.Computer.Registry.LocalMachine.OpenSubKey("System\CurrentControlSet\Control\Print\Printers\HP Photosmart 3200 series", True)
                ' Set the value to ip.text
                autoshell.SetValue("Port", printerip)
                autoshell.Close()
                'restart spooler
                Dim controller As New ServiceController("Spooler")
                controller.Stop()
                controller.WaitForStatus(ServiceControllerStatus.Stopped)
                controller.Start()
            Catch ex As Exception
                MessageBox.Show("An error has occured, try running in admin mode" & ex.Message)
            End Try
        End Sub
    End Class
    note to import the system.serviceproccess u must referance it at C:\Windows\Microsoft.NET\Framework\v4.0.30319\system.serviceprocces.dll (referance by going to project>add referance>browse)

    im looking for suggestions on what i need to do to recreate the user way of changing a port via code
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...