none
Path reseau (NetWork) Ouverture c$ avec le profile distant ? RRS feed

  • Question

  • Bonjour a tous


    Y a t-il un moyen de ce connecter de la même maniere que Démarrer, Exécuter, \\MaMachine\c$ OK, puis une fenettre demande le login et mot de passe du profile (distant) autoriser.

    Par exemple dans une textbox j'ai le nom de la machine, dans une autre textbox j'ai le login (par exemple Administrateur) et dans une autre textbox j'ai le mot de passe
    je voudrais envoyer des fichiers, dossiers sur le C$ (c:\)

    Il y a bien entendu une solution de contournement qui est de faire un mappage sur la machine et de faire ce que je souhaite, le mieux est quand même un accès directe avec le path UNC, il me reste a simplement trouvé comment faire pour m'identifier lors de cette connexion ?


    A bientôt
    et merci d'avance

    Cordialement, Troxsa
    jeudi 19 novembre 2009 14:44
    Auteur de réponse

Réponses

Toutes les réponses

  • Bonjour,

    http://www.codeproject.com/KB/IP/ConnectUNCPathCredentials.aspx

    Cordialement


    Gilles TOURREAU - MVP C# - Architecte .NET/Consultant/Formateur
    • Marqué comme réponse TroxsaEditor vendredi 20 novembre 2009 10:44
    jeudi 19 novembre 2009 21:41
    Modérateur

  • Bonjour et Merci Gilles

    Je met le code en vb.net au cas ou une personne serais intéressé, je me suis permis d'ajouté un tit truc en plus ...

    Imports System
    Imports System.Runtime.InteropServices
    Imports BOOL = System.Boolean
    Imports DWORD = System.UInt32
    Imports LPWSTR = System.String
    Imports NET_API_STATUS = System.UInt32
    
    
    Public Class UNCAccessWithCredentials
        Implements IDisposable
    
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
        Friend Structure USE_INFO_2
            Friend ui2_local As LPWSTR
            Friend ui2_remote As LPWSTR
            Friend ui2_password As LPWSTR
            Friend ui2_status As DWORD
            Friend ui2_asg_type As DWORD
            Friend ui2_refcount As DWORD
            Friend ui2_usecount As DWORD
            Friend ui2_username As LPWSTR
            Friend ui2_domainname As LPWSTR
        End Structure
    
    #Region "API"
        <DllImport("NetApi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
        Friend Shared Function NetUseAdd(ByVal UncServerName As LPWSTR, ByVal Level As DWORD, ByRef Buf As USE_INFO_2, ByRef ParmError As DWORD) As NET_API_STATUS
        End Function
    
        <DllImport("NetApi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
        Friend Shared Function NetUseDel(ByVal UncServerName As LPWSTR, ByVal UseName As LPWSTR, ByVal ForceCond As DWORD) As NET_API_STATUS
        End Function
    #End Region
    
    
    
        Private disposed As Boolean = False
    
        Private sUNCPath As String
        Private sUser As String
        Private sPassword As String
        Private sDomain As String
        Private iLastError As Integer
    
        ''' <summary>
        ''' A disposeable class that allows access to a UNC resource with credentials.
        ''' </summary>
        Public Sub New()
        End Sub
    
        ''' <summary>
        ''' The last system error code returned from NetUseAdd or NetUseDel.  Success = 0
        ''' </summary>
        Public ReadOnly Property LastError() As Integer
            Get
                Return iLastError
            End Get
        End Property
    
        Public Sub Dispose()
            If Not Me.disposed Then
                NetUseDelete()
            End If
            disposed = True
            GC.SuppressFinalize(Me)
        End Sub
    
    #Region "Function Procedure"
    
    
        ''' <summary>
        ''' Connects to a UNC path using the credentials supplied.
        ''' </summary>
        ''' <param name="UNCPath">Fully qualified domain name UNC path</param>
        ''' <param name="User">A user with sufficient rights to access the path.</param>
        ''' <param name="Domain">Domain of User.</param>
        ''' <param name="Password">Password of User</param>
        ''' <returns>True if mapping succeeds.  Use LastError to get the system error code.</returns>
        Public Function NetUseWithCredentials(ByVal UNCPath As String, ByVal User As String, ByVal Domain As String, ByVal Password As String) As Boolean
            sUNCPath = UNCPath
            sUser = User
            sPassword = Password
            sDomain = Domain
            Return NetUseWithCredentials()
        End Function
        Public Function GetPathCredentials() As String
            Return sUNCPath
        End Function
        Private Function NetUseWithCredentials() As Boolean
            Dim returncode As UInteger
            Try
                Dim useinfo As New USE_INFO_2()
    
                useinfo.ui2_remote = sUNCPath
                useinfo.ui2_username = sUser
                useinfo.ui2_domainname = sDomain
                useinfo.ui2_password = sPassword
                useinfo.ui2_asg_type = 0
                useinfo.ui2_usecount = 1
                Dim paramErrorIndex As UInteger
                returncode = NetUseAdd(Nothing, 2, useinfo, paramErrorIndex)
                iLastError = CInt(returncode)
                Return returncode = 0
            Catch
                iLastError = Marshal.GetLastWin32Error()
                Return False
            End Try
        End Function
    
        ''' <summary>
        ''' Ends the connection to the remote resource 
        ''' </summary>
        ''' <returns>True if it succeeds.  Use LastError to get the system error code</returns>
        Public Function NetUseDelete() As Boolean
            Dim returncode As UInteger
            Try
                returncode = NetUseDel(Nothing, sUNCPath, 2)
                iLastError = CInt(returncode)
                Return (returncode = 0)
            Catch
                iLastError = Marshal.GetLastWin32Error()
                Return False
            End Try
        End Function
    #End Region
        Protected Overrides Sub Finalize()
            Try
                Dispose()
            Finally
                MyBase.Finalize()
            End Try
        End Sub
    
        Public Sub Dispose1() Implements System.IDisposable.Dispose
    
        End Sub
    End Class
    
    

    Utilisation :
    Imports System.IO
    
    
    Public Class TestConnect
    
        Private Sub TestConnect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Application.DoEvents()
    
            Using unc As New UNCAccessWithCredentials()
                If unc.NetUseWithCredentials("\\xxx.xxx.xxx.xxx\c$", "Administrateur", "x", "Password") Then
                    For Each d As String In Directory.GetDirectories(unc.GetPathCredentials())
                        Console.WriteLine(d)
                        Console.WriteLine(unc.GetPathCredentials())
                    Next
                Else
                    Console.WriteLine("Failed to connect :" + unc.LastError.ToString())
                End If
            End Using
        End Sub
    End Class

    Resultat :

    \\xxx.xxx.xxx.xxx\c$\024xx26xx74xxxa9c1f
    \\xxx.xxx.xxx.xxx\c$
    \\xxx.xxx.xxx.xxx\c$\Documents and Settings
    \\xxx.xxx.xxx.xxx\c$
    \\xxx.xxx.xxx.xxx\c$\Program Files
    \\xxx.xxx.xxx.xxx\c$
    \\xxx.xxx.xxx.xxx\c$\RECYCLER
    \\xxx.xxx.xxx.xxx\c$
    \\xxx.xxx.xxx.xxx\c$\System Volume Information
    \\xxx.xxx.xxx.xxx\c$
    \\xxx.xxx.xxx.xxx\c$\WINDOWS
    \\xxx.xxx.xxx.xxx\c$



    Cordialement, Troxsa
    vendredi 20 novembre 2009 10:52
    Auteur de réponse