By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,065 Members | 1,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,065 IT Pros & Developers. It's quick & easy.

System.Management.ManagementObject cannot set property.

P: n/a
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
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.