I am using the System.Management namespace for the first time and I
cannot get the ManagementObject.SetPropertyValue method to work
properly.
I am trying to change the ServerName and ShareName of a printer, to
facilitate changing the print server of workstations remotely in the
event of a print server failure.
Thanks in advance for any assistance.
Here is my subroutine (None of the error catches are triggered in normal
execution, but the SetPropertyValue lines just seem to have no result at
all):
Private Sub btnSaveSetting_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSaveSetting.Click
Dim query As ManagementObjectSearcher
Dim queryCollection As ManagementObjectCollection
Dim co As ConnectionOptions
Dim oq As ObjectQuery
Dim ms As ManagementScope
Dim mo As ManagementObject
Dim strComputerName, strQuery As String
Dim sPrinterInfo As String = ""
lblComputer.Text = sComputerName + sPrinterName
If sPrinterName.Trim() = "" Then
Exit Sub
End If
strQuery = "SELECT * FROM Win32_Printer"
strComputerName = txtServerName.Text
Cursor.Current = Cursors.WaitCursor
co = New ConnectionOptions
If (strComputerName.Trim().Length = 0) Then
Cursor.Current = Cursors.Default
MessageBox.Show("Must enter machine IP address or name.")
Else
ms = New System.Management.ManagementScope("\\" + strComputerName +
"\root\cimv2", co)
oq = New System.Management.ObjectQuery(strQuery)
query = New ManagementObjectSearcher(ms, oq)
Try
queryCollection = query.Get()
Catch e1 As Exception
sendResult("Error: " + e1.ToString())
End Try
For Each mo In queryCollection
Try
If mo("Name").ToString() = sPrinterName Then
Try
mo.SetPropertyValue("Name", txtName.Text)
Catch ex As Exception
sendResult("Error: (Could not set Name) " + ex.ToString)
End Try
Try
mo.SetPropertyValue("ServerName", txtServer.Text)
Catch ex As Exception
sendResult("Error: (Could not set ServerName) " + ex.ToString)
End Try
Try
mo.SetPropertyValue("ShareName", cboxShareName.Text)
Catch ex As Exception
sendResult("Error: (Could not set ShareName) " + ex.ToString)
End Try
End If
Catch ex As Exception
sendResult("Error: " + ex.ToString())
End Try
Next
End If
Cursor.Current = Cursors.Default
GC.Collect()
End Sub