设置IP地址& date使用asp.net的服务器的时间

时间:2011-07-05 12:03:47

标签: .net asp.net vb.net

我必须编写一些代码来设置PC的IP地址。当我从代码运行应用程序时它工作正常,但问题是当我发布网站时,它显示IP已成功设置,但实际上并非如此。

代码是:

Private Sub SetIP(ByVal IPAddress As String, ByVal SubnetMask As String, _
                  ByVal Gateway As String)

    Dim managementClass As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim mgObjCollection As ManagementObjectCollection = managementClass.GetInstances()

    For Each mgObject As ManagementObject In mgObjCollection
        If Not CType(mgObject("IPEnabled"), Boolean) Then Continue For

        Try
            Dim objNewIP As ManagementBaseObject = Nothing
            Dim objSetIP As ManagementBaseObject = Nothing
            Dim objNewGate As ManagementBaseObject = Nothing

            objNewIP = mgObject.GetMethodParameters("EnableStatic")
            objNewGate = mgObject.GetMethodParameters("SetGateways")

            ' Set the default gateway (decided to declare and initialise
            ' variables rather than attempting to initialize the array
            ' while communicating with the WMI.
            Dim tmpStrArray() As String = {Gateway}

            objNewGate("DefaultIPGateway") = tmpStrArray
            Dim tmpIntArray() As Integer = {1}
            objNewGate("GatewayCostMetric") = tmpIntArray

            ' Set the IP address and subnet.
            tmpStrArray(0) = IPAddress
            objNewIP("IPAddress") = tmpStrArray
            tmpStrArray(0) = SubnetMask
            objNewIP("SubnetMask") = tmpStrArray

            objSetIP = mgObject.InvokeMethod("EnableStatic", objNewIP, Nothing)
            objSetIP = mgObject.InvokeMethod("SetGateways", objNewGate, Nothing)
        Catch ex As Exception
            MessageBox.Show("An error occured: " + ex.Message)
        End Try
    Next
End Sub

我正在使用它:

 Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim i As Integer
    i = Set_IP("10.17.1.148", "255.255.240.0", "10.17.1.21")
    If i = 1 Then
        Label1.Text = "IP SET"
    Else
        Label1.Text = "Fail To Set !"
    End If
End Sub
Public Function Set_IP(ByVal IPAddress As String, ByVal SubnetMask As String, _
                 ByVal Gateway As String)
    Dim managementClass As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim mgObjCollection As ManagementObjectCollection = managementClass.GetInstances()
    Dim Flag As Boolean

    For Each mgObject As ManagementObject In mgObjCollection
        If Not CType(mgObject("IPEnabled"), Boolean) Then Continue For

        Try
            Dim objNewIP As ManagementBaseObject = Nothing
            Dim objSetIP As ManagementBaseObject = Nothing
            Dim objNewGate As ManagementBaseObject = Nothing

            objNewIP = mgObject.GetMethodParameters("EnableStatic")
            objNewGate = mgObject.GetMethodParameters("SetGateways")

            ' Set the default gateway (decided to declare and initialise
            ' variables rather than attempting to initialize the array
            ' while communicating with the WMI.
            Dim tmpStrArray() As String = {Gateway}

            objNewGate("DefaultIPGateway") = tmpStrArray
            Dim tmpIntArray() As Integer = {1}
            objNewGate("GatewayCostMetric") = tmpIntArray

            ' Set the IP address and subnet.
            tmpStrArray(0) = IPAddress
            objNewIP("IPAddress") = tmpStrArray
            tmpStrArray(0) = SubnetMask
            objNewIP("SubnetMask") = tmpStrArray

            objSetIP = mgObject.InvokeMethod("EnableStatic", objNewIP, Nothing)
            objSetIP = mgObject.InvokeMethod("SetGateways", objNewGate, Nothing)
            Flag = True
        Catch ex As Exception
            'MessageBox.Show("An error occured: " + ex.Message)
            Flag = False
        End Try
    Next
    If Flag = True Then
        Return 1
    Else
        Return 0
    End If
End Function

0 个答案:

没有答案