2010年12月23日

VB.NET修改電腦名稱

如果要在程式裡修改電腦名稱, 可以用以下指令:
Public Class Form1

Public Class Form1 
  
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
  Try
 Dim oldComputerName As String = "oldComputerName" '舊電腦名稱
 Dim newComputerName As String = "newComputerName" '新電腦名稱
 
  
 Dim classInstance As New ManagementObject( _ 
    "root\CIMV2", _ 
    "Win32_ComputerSystem.Name='" & oldComputerName & "'", Nothing) 
    Dim inParams As ManagementBaseObject = _ 
    classInstance.GetMethodParameters("Rename") 
    inParams("Name") = newComputerName 'rename to 
    inParams("Password") = "administrator" '使用者代號 
    inParams("UserName") = "XXXX" '使用者密碼
    Dim outParams As ManagementBaseObject = _ 
    classInstance.InvokeMethod("Rename", inParams,Nothing) 
    MessageBox.Show("ReturnValue: {0}", outParams("ReturnValue")) 
    Catch err As ManagementException 
    MessageBox.Show("Error: " & err.Message) 
  End Try
  
 End Sub
End Class

沒有留言:

張貼留言