Ciao
i files .bat li uso e funzionano bene. Volevo qualcosa di diverso
Ho risolto così:
codice:
Imports System.Xml
Imports System.IO
Module Module1
Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Integer) As Integer
Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
Structure NETRESOURCE
Dim dwScope As Integer
Dim dwType As Integer
Dim dwDisplayType As Integer
Dim dwUsage As Integer
Dim lpLocalName As String
Dim lpRemoteName As String
Dim lpComment As String
Dim lpProvider As String
End Structure
Sub Main()
Dim da As String = My.Application.Info.DirectoryPath
Dim filexml As String = Path.Combine(da, "rete.xml")
'istanzio l'oggetto xml DOM
Dim xmlDoc As New XmlDocument()
Try
'creo il DOM leggendo il file xml
xmlDoc.Load(filexml)
Catch ex As Exception
MsgBox(ex.Message)
Return
End Try
'accedo alla radice del documento
Dim radice As XmlElement = xmlDoc.DocumentElement()
Dim nodi As XmlNodeList = Nothing
Dim nodo As XmlNode = Nothing
'tutti i nodi di primo livello libro che partono da radice
nodi = radice.SelectNodes("./server")
Dim result As String = ""
Dim Wnet As Integer
For i As Integer = 0 To nodi.Count - 1
nodo = nodi(i)
Dim drive As String = ""
Dim indirizzo As String = ""
Dim utente As String = ""
Dim password As String = ""
Dim flag As Integer = 0
Dim lpNetResource As NETRESOURCE
Try
drive = nodo.SelectSingleNode("drive").FirstChild().Value
indirizzo = nodo.SelectSingleNode("indirizzo").FirstChild().Value
utente = nodo.SelectSingleNode("utente").FirstChild().Value
password = nodo.SelectSingleNode("password").FirstChild().Value
flag = CInt(nodo.SelectSingleNode("flag").FirstChild().Value)
lpNetResource.lpLocalName = drive
lpNetResource.lpRemoteName = indirizzo
Wnet = WNetAddConnection2(lpNetResource, password, utente, flag)
If Wnet <> 0 AndAlso Wnet <> 85 Then '85=The local device name is already in use
Throw (New Exception(String.Format("Errore connessione: {0}", indirizzo)))
End If
Catch ex As Exception
result &= ex.Message & vbNewLine
End Try
Next
If result = "" Then
result = "Connessione riuscita"
End If
MsgBox(result)
End Sub
End Module
il file xml lo scrivo a mano ed ha la struttura:
codice:
<?xml version="1.0" encoding="utf-8"?>
<servers>
<server>
<drive>p:</drive>
<indirizzo>\\xxxxx</indirizzo>
<utente>xxx</utente>
<password>xxx</password>
<flag>0</flag>
</server>
</servers>
la disconnessione la faccio con:
codice:
Module Module1
Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Integer) As Integer
Sub Main()
Dim disco As String = ""
For i As Integer = 97 To 122
disco = Chr(i) & ":"
WNetCancelConnection(disco, -1)
Next
MsgBox("Rete disconnessa")
End Sub
End Module
Pensavo che ci fosse qualche classe specializzata tale da non ricorrere alle API.
Ciao