Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - impostazioni lan
Forum - C# / VB.NET - impostazioni lan

Avatar
dravolo (Normal User)
Newbie


Messaggi: 2
Iscritto: 26/04/2013

Segnala al moderatore
Postato alle 18:15
Venerdì, 26/04/2013
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 è:

Codice sorgente - presumibilmente VB.NET

  1. Imports System.Net
  2. Imports System.IO
  3.  
  4.  
  5. Public Class Form1
  6.  
  7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  8.  
  9.         Dim comandoip = "netsh interface ip set address ""Connessione alla rete locale (LAN)"" dhcp"
  10.         Shell(comandoip, vbHide)
  11.  
  12.         Dim comandodns = "netsh interface ip set dns ""Connessione alla rete locale (LAN)"" dhcp"
  13.         Shell(comandodns, vbHide)
  14.         Label2.Hide()
  15.         Label3.Hide()
  16.         Label1.Show()
  17.  
  18.     End Sub
  19.  
  20.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  21.        
  22.     End Sub
  23.  
  24.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  25.  
  26.  
  27.         Dim hostName As String = System.Net.Dns.GetHostName()
  28.  
  29.         Dim lettura As String = ""
  30.         Dim f As New IO.StreamReader("indirizzi.txt")
  31.         ' peek ritorna il prossimo carattere dello stream. se -1 sono alla fine
  32.  
  33.         While f.Peek <> -1
  34.             lettura = f.ReadLine
  35.             If lettura.Contains(hostName) Then
  36.                 Dim lunghezzaHost As Integer = Len(hostName)
  37.                 Dim recuperaIP As String = lettura.Substring(lunghezzaHost + 1)
  38.                 Dim comandoip = "netsh interface ip set address ""Connessione alla rete locale (LAN)"" static " & recuperaIP & " " & "255.255.255.0 10.49.28.253 1"
  39.                 Shell(comandoip, vbHide)
  40.                 Label3.Text = "Identificato con indirizzo IP: " & recuperaIP
  41.             End If
  42.         End While
  43.  
  44.         f.Close()
  45.  
  46.         Dim comandodns = "netsh interface ip add dns ""Connessione alla rete locale (LAN)"" 212.216.112.112"
  47.         Shell(comandodns, vbHide)
  48.  
  49.         Dim comandodns2 = "netsh interface ip add dns ""Connessione alla rete locale (LAN)"" 10.49.28.246 index=2"
  50.         Shell(comandodns2, vbHide)
  51.  
  52.         Label1.Hide()
  53.         Label3.Show()
  54.         Label2.Show()
  55.  
  56.     End Sub
  57.  
  58.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  59.         Me.Close()
  60.     End Sub
  61. End Class



qualcuno potrebbe aiutarmi? grazie in anticipo

PM Quote
Avatar
lillogoal (Member)
Rookie


Messaggi: 28
Iscritto: 26/04/2013

Segnala al moderatore
Postato alle 23:46
Venerdì, 26/04/2013
Hai provato su altri pc con windows xp? o windows 8? potrebbe essere il framework con cui hai compilato il progetto :)

PM Quote
Avatar
dravolo (Normal User)
Newbie


Messaggi: 2
Iscritto: 26/04/2013

Segnala al moderatore
Postato alle 13:36
Lunedì, 29/04/2013
l'ho provato su due diverse macchine xp installando prima lo stesso framework installato sulla macchina windows 7 dove ho creato il programmino

PM Quote
Avatar
ampeg (Normal User)
Pro


Messaggi: 124
Iscritto: 21/04/2011

Segnala al moderatore
Postato alle 17:25
Lunedì, 29/04/2013
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


http://msdn.microsoft.com/it-it/library/system.diagnostics ...

http://msdn.microsoft.com/it-it/library/fb4aw7b8.aspx

PM Quote
Avatar
Neo1986 (Ex-Member)
Pro


Messaggi: 97
Iscritto: 21/06/2010

Segnala al moderatore
Postato alle 12:33
Giovedì, 30/05/2013
Secondo me non è molto bello utilizzare un comando dal prompt
per cambiare l'ip.

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

Codice sorgente - presumibilmente VB.NET

  1. Private Sub ButtonCambia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCambia.Click
  2.  
  3.         Dim IPAddress As String = Me.TextBoxIndirizzoIP.Text
  4.  
  5.         Dim SubnetMask As String = Me.TextBoxSubnet.Text
  6.  
  7.         Dim Gateway As String = Me.TextBoxGateway.Text
  8.  
  9.         Dim IndirizzoDNS As String = Me.TextBoxDNS.Text & "," & Me.TextBoxDNS.Text
  10.  
  11.  
  12.  
  13.         Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
  14.  
  15.         Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
  16.  
  17.  
  18.  
  19.         For Each objMO As ManagementObject In objMOC
  20.  
  21.             If (Not CBool(objMO("IPEnabled"))) Then
  22.  
  23.                 Continue For
  24.  
  25.             End If
  26.  
  27.  
  28.  
  29.             Try
  30.  
  31.                 Dim objNewIP As ManagementBaseObject = Nothing
  32.  
  33.                 Dim objSetIP As ManagementBaseObject = Nothing
  34.  
  35.                 Dim objNewGate As ManagementBaseObject = Nothing
  36.  
  37.                 Dim objNewDNS As ManagementBaseObject = Nothing
  38.  
  39.  
  40.  
  41.                 objNewIP = objMO.GetMethodParameters("EnableStatic")
  42.  
  43.                 objNewGate = objMO.GetMethodParameters("SetGateways")
  44.  
  45.                 objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder")
  46.  
  47.                 'Set DefaultGateway
  48.  
  49.                 objNewGate("DefaultIPGateway") = New String() {Gateway}
  50.  
  51.                 objNewGate("GatewayCostMetric") = New Integer() {1}
  52.  
  53.  
  54.  
  55.                 'Set IPAddress and Subnet Mask
  56.  
  57.                 objNewIP("IPAddress") = New String() {IPAddress}
  58.  
  59.                 objNewIP("SubnetMask") = New String() {SubnetMask}
  60.  
  61.  
  62.                 'set DNS address
  63.  
  64.                 objNewDNS("DNSServerSearchOrder") = IndirizzoDNS.Split(",")
  65.  
  66.  
  67.  
  68.                 objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing)
  69.  
  70.                 objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)
  71.  
  72.                 objSetIP = objMO.InvokeMethod("SetDNSServerSearchOrder", objNewDNS, Nothing)
  73.  
  74.                 Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!")
  75.  
  76.  
  77.  
  78.             Catch ex As Exception
  79.  
  80.                 MessageBox.Show("Unable to Set IP : " & ex.Message)
  81.  
  82.             End Try
  83.  
  84.         Next objMO
  85.  
  86.  
  87.     End Sub



Ricordati Imports System.Management

Spero di esserti stato utile

PM Quote