Oppure

Loading
26/04/13 16:15
dravolo
buongiorno a tutti, ho la necessità, tramite un piccolo programmino di switchare automaticamente la configurazione della scheda di rete, una con dhcp e l'altra con indirizzo statico che prendo da un file di testo del tipo:
pc1;192.168.1.100
pc2;192.168.1.101
ecc..
ho creato questo programmino (il mio primo programma in vb net quindi scusatemi per ogni eventuale sbaglio). L'ho creato con VB.Net 2010 Express sotto windows 7 e funziona perfettamente. Lo stesso programma sotto windows xp sp3 con .net framework 4 funziona ma male, nel senso che per attivare la configurazione sulla scheda di rete devo cliccare più volte sui button del programma.
il pezzo di codice che riguarda questa funzione è:

Imports System.Net
Imports System.IO


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim comandoip = "netsh interface ip set address ""Connessione alla rete locale (LAN)"" dhcp"
        Shell(comandoip, vbHide)

        Dim comandodns = "netsh interface ip set dns ""Connessione alla rete locale (LAN)"" dhcp"
        Shell(comandodns, vbHide)
        Label2.Hide()
        Label3.Hide()
        Label1.Show()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


        Dim hostName As String = System.Net.Dns.GetHostName()

        Dim lettura As String = ""
        Dim f As New IO.StreamReader("indirizzi.txt")
        ' peek ritorna il prossimo carattere dello stream. se -1 sono alla fine

        While f.Peek <> -1
            lettura = f.ReadLine
            If lettura.Contains(hostName) Then
                Dim lunghezzaHost As Integer = Len(hostName)
                Dim recuperaIP As String = lettura.Substring(lunghezzaHost + 1)
                Dim comandoip = "netsh interface ip set address ""Connessione alla rete locale (LAN)"" static " & recuperaIP & " " & "255.255.255.0 10.49.28.253 1"
                Shell(comandoip, vbHide)
                Label3.Text = "Identificato con indirizzo IP: " & recuperaIP
            End If
        End While

        f.Close()

        Dim comandodns = "netsh interface ip add dns ""Connessione alla rete locale (LAN)"" 212.216.112.112"
        Shell(comandodns, vbHide)

        Dim comandodns2 = "netsh interface ip add dns ""Connessione alla rete locale (LAN)"" 10.49.28.246 index=2"
        Shell(comandodns2, vbHide)

        Label1.Hide()
        Label3.Show()
        Label2.Show()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub
End Class


qualcuno potrebbe aiutarmi? grazie in anticipo
aaa
26/04/13 21:46
lillogoal
Hai provato su altri pc con windows xp? o windows 8? potrebbe essere il framework con cui hai compilato il progetto :)
aaa
29/04/13 11:36
dravolo
l'ho provato su due diverse macchine xp installando prima lo stesso framework installato sulla macchina windows 7 dove ho creato il programmino
aaa
29/04/13 15:25
ampeg
vedo che esegui netsh in un loop con il comando shell che lo lancia in modo asincrono e questo potrebbe essere la causa del "malfunzionamento"

prova ad usare la classe Process e il metodo WaitForExit della medesima classe

in pratica anziché lanciare in modo asincrono il netsh, il metodo attende che termini la sessione precedente prima di rilanciarlo


msdn.microsoft.com/it-it/library/…

msdn.microsoft.com/it-it/library/…
aaa
30/05/13 10:33
Neo1986
Secondo me non è molto bello utilizzare un comando dal prompt
per cambiare l'ip.

Guarda questo codice, effettua proprio quello che devi fare tu :


Private Sub ButtonCambia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCambia.Click

        Dim IPAddress As String = Me.TextBoxIndirizzoIP.Text

        Dim SubnetMask As String = Me.TextBoxSubnet.Text

        Dim Gateway As String = Me.TextBoxGateway.Text

        Dim IndirizzoDNS As String = Me.TextBoxDNS.Text & "," & Me.TextBoxDNS.Text



        Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")

        Dim objMOC As ManagementObjectCollection = objMC.GetInstances()



        For Each objMO As ManagementObject In objMOC

            If (Not CBool(objMO("IPEnabled"))) Then

                Continue For

            End If



            Try

                Dim objNewIP As ManagementBaseObject = Nothing

                Dim objSetIP As ManagementBaseObject = Nothing

                Dim objNewGate As ManagementBaseObject = Nothing

                Dim objNewDNS As ManagementBaseObject = Nothing



                objNewIP = objMO.GetMethodParameters("EnableStatic")

                objNewGate = objMO.GetMethodParameters("SetGateways")

                objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder")

                'Set DefaultGateway

                objNewGate("DefaultIPGateway") = New String() {Gateway}

                objNewGate("GatewayCostMetric") = New Integer() {1}



                'Set IPAddress and Subnet Mask

                objNewIP("IPAddress") = New String() {IPAddress}

                objNewIP("SubnetMask") = New String() {SubnetMask}


                'set DNS address

                objNewDNS("DNSServerSearchOrder") = IndirizzoDNS.Split(",")



                objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing)

                objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)

                objSetIP = objMO.InvokeMethod("SetDNSServerSearchOrder", objNewDNS, Nothing)

                Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!")



            Catch ex As Exception

                MessageBox.Show("Unable to Set IP : " & ex.Message)

            End Try

        Next objMO


    End Sub



Ricordati Imports System.Management

Spero di esserti stato utile
aaa