Accès à un répertoire distant protégé

kamui74 Messages postés 29 Date d'inscription vendredi 4 mars 2005 Statut Membre Dernière intervention 26 décembre 2006 - 12 juil. 2005 à 12:53
kamui74 Messages postés 29 Date d'inscription vendredi 4 mars 2005 Statut Membre Dernière intervention 26 décembre 2006 - 12 juil. 2005 à 14:13
Salut, je suis en train de développer une application en VB.Net et je dois récupérer des fichiers qui sont sur le disque dur d'un automate. Le disque est accessible via le réseau comme n'importe quel disque partagé, mais on doit s'authentifier pour pouvoir y acceder.

Dans mon application, j'utilise un fileSystemObject et je veux aller lire les fichiers de ce fameux répertoire. En gros je cherche à faire ça :


Dim fso As New Scripting.FileSystemObject
Dim fol As Scripting.Folder
Dim files As Scripting.Files
Dim f As Scripting.File
fol = fso.GetFolder("\\Automate\Projet")
files = fol.Files
Txt.Clear()
For Each f In files
Txt.Text &= (f.Name) & vbNewLine
Analyse(f.Name)

Next

Si par exemple je me suis connecté en donnant login + mdp avant, via Explorer ça marche, mais je voudrais que ça soit indépendant.

Si qqu à une idée, je suis preneur ! Merci

Kamui74

3 réponses

gaa179 Messages postés 361 Date d'inscription mercredi 21 mai 2003 Statut Membre Dernière intervention 12 novembre 2009 2
12 juil. 2005 à 13:07
Salut,

tu peux utiliser les Apis et WnetAddConnection pour créer le mapping du share.
Voici la syntaxe que tu dois placer dans un module:

Const RESOURCETYPE_DISK = &H1


Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_BAD_NET_NAME = 67&
Public Const ERROR_DEVICE_IN_USE = 2404&
Public Const ERROR_INVALID_PASSWORD = 86&
Public Const ERROR_MORE_DATA = 234 ' dderror
Public Const ERROR_NO_NETWORK = 1222&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Public Const ERROR_NOT_CONNECTED = 2250&
Public Const NO_ERROR = 0 ' dderror


Public Const WN_ACCESS_DENIED = ERROR_ACCESS_DENIED
Public Const WN_ALREADY_CONNECTED = ERROR_ALREADY_ASSIGNED
Public Const WN_BAD_LOCALNAME = ERROR_BAD_DEVICE
Public Const WN_BAD_NETNAME = ERROR_BAD_NET_NAME
Public Const WN_BAD_PASSWORD = ERROR_INVALID_PASSWORD
Public Const WN_DEVICE_IN_USE = ERROR_DEVICE_IN_USE
Public Const WN_MORE_DATA = ERROR_MORE_DATA
Public Const WN_NO_ERROR = NO_ERROR
Public Const WN_NO_NET_OR_BAD_PATH = ERROR_NO_NET_OR_BAD_PATH
Public Const WN_NO_NETWORK = ERROR_NO_NETWORK
Public Const WN_NOT_CONNECTED = ERROR_NOT_CONNECTED
'Public Const WN_BAD_PROFILE = ERROR_BAD_PROFILE


Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type


Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long


Public Function MapDrive(LocalDrive As String, RemoteName As String, UserName as String, UserPassword as String) As Long
Dim theNetResource As NETRESOURCE
Dim UserName As String
Dim UserPassword As String
'UserName = Null
'UserPassword = Null
With theNetResource
.dwType = RESOURCETYPE_DISK
.lpLocalName = LocalDrive
.lpRemoteName = RemoteName
End With
MapDrive = WNetAddConnection2(theNetResource, UserPassword, UserName, 0)
End Function
Public Function DisconnectDrive(LocalDrive As String) As Long
DisconnectDrive = WNetCancelConnection2(LocalDrive, 0, 0)
End Function


A+
0
kamui74 Messages postés 29 Date d'inscription vendredi 4 mars 2005 Statut Membre Dernière intervention 26 décembre 2006
12 juil. 2005 à 14:11
j'ai mis ton code dans un module et j'ai appellé la fonction:


Mapdrive("Z:", "[file://automate/projet/ \\automate\projet\]", "Administrator", "Administrator")

Normalement à partir de ce moment je suis sensé voir un nouveau disque réseau dan sle poste de travail, non ? En tout cas il ne se passe rien et quand je cherche à accéder aux fichier, il me dit que le chemin est inconnu (normal si c'est bien sensé faire ce que je pense)

J'ai du adapter un peu le type remplacé par Structure en .NET et il y a des déclarations en double que j'ai enlevé. Est-ce que ça serait ça qui pose problème?

PS: Je suis sous Windows 2000 !
0
kamui74 Messages postés 29 Date d'inscription vendredi 4 mars 2005 Statut Membre Dernière intervention 26 décembre 2006
12 juil. 2005 à 14:13
J'ai aussi essayé avec :

Mapdrive("Z:", "[file://automate/projet/ \\automate\projet\]", "Administrator", "Administrator")

Même punition !
0
Rejoignez-nous