IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

service de copie de fichier - Accès à dossiers distants [Débutant]


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    362
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 362
    Par défaut service de copie de fichier - Accès à dossiers distants
    Bonjour à tous,

    Je viens de créé un petit service pour déplacer un type de fichier (.txt par exemple) d'un dossier A vers un dossier B. Cela fonctionne nickel pour des dossiers en local sur mon PC.

    Maintenant je souhaite l'améliorer afin de pouvoir accéder également à des dossiers partagés distants avec éventuellement les login/password.

    Par contre je ne vois pas du tout comment aborder ce problème. Avez-vous des exemples de sources à me fournir?

    A l'avance merci

  2. #2
    Membre expérimenté Avatar de diallomad
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    164
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Mali

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 164
    Par défaut
    Salut, tu peux utiliser WNetAddConnection3 pour l'authentification réseau.

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    362
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 362
    Par défaut
    Salut

    je vais jeter un oeil dessus.

    Aurais-tu eventuellement des liens vers des exemples, des sources?

    merci

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    362
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 362
    Par défaut
    personne n'aurait une source ou des exemples?

  5. #5
    Membre expérimenté Avatar de diallomad
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    164
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Mali

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 164
    Par défaut
    Salut, tu peux essayer ça.

  6. #6
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    362
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 362
    Par défaut
    Salut,

    oui j'ai fait ce genre de recherche et je suis tombé sur ce code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
     
    using System;
    using System.Runtime.InteropServices;
    using BOOL = System.Boolean;
    using DWORD = System.UInt32;
    using LPWSTR = System.String;
    using NET_API_STATUS = System.UInt32;
     
    namespace MoveFile
    {
        public class UNCAccessWithCredentials : IDisposable
        {
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            internal struct USE_INFO_2
            {
                internal LPWSTR ui2_local;
                internal LPWSTR ui2_remote;
                internal LPWSTR ui2_password;
                internal DWORD ui2_status;
                internal DWORD ui2_asg_type;
                internal DWORD ui2_refcount;
                internal DWORD ui2_usecount;
                internal LPWSTR ui2_username;
                internal LPWSTR ui2_domainname;
            }
     
            [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            internal static extern NET_API_STATUS NetUseAdd(
                LPWSTR UncServerName,
                DWORD Level,
                ref USE_INFO_2 Buf,
                out DWORD ParmError);
     
            [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            internal static extern NET_API_STATUS NetUseDel(
                LPWSTR UncServerName,
                LPWSTR UseName,
                DWORD ForceCond);
     
            private bool disposed = false;
     
            private string sUNCPath;
            private string sUser;
            private string sPassword;
            private string sDomain;
            private int iLastError;
     
            /// <summary>
            /// A disposeable class that allows access to a UNC resource with credentials.
            /// </summary>
            public UNCAccessWithCredentials()
            {
            }
     
            /// <summary>
            /// The last system error code returned from NetUseAdd or NetUseDel.  Success = 0
            /// </summary>
            public int LastError
            {
                get { return iLastError; }
            }
     
            public void Dispose()
            {
                if (!this.disposed)
                {
                    NetUseDelete();
                }
                disposed = true;
                GC.SuppressFinalize(this);
            }
     
            /// <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 bool NetUseWithCredentials(string UNCPath, string User, string Domain, string Password)
            {
                sUNCPath = UNCPath;
                sUser = User;
                sPassword = Password;
                sDomain = Domain;
                return NetUseWithCredentials();
            }
     
            private bool NetUseWithCredentials()
            {
                uint returncode;
                try
                {
                    USE_INFO_2 useinfo = 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;
                    uint paramErrorIndex;
                    returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                    iLastError = (int)returncode;
                    return returncode == 0;
                }
                catch
                {
                    iLastError = Marshal.GetLastWin32Error();
                    return false;
                }
            }
     
            /// <summary>
            /// Ends the connection to the remote resource 
            /// </summary>
            /// <returns>True if it succeeds.  Use LastError to get the system error code</returns>
            public bool NetUseDelete()
            {
                uint returncode;
                try
                {
                    returncode = NetUseDel(null, sUNCPath, 2);
                    iLastError = (int)returncode;
                    return (returncode == 0);
                }
                catch
                {
                    iLastError = Marshal.GetLastWin32Error();
                    return false;
                }
            }
     
            ~UNCAccessWithCredentials()
            {
                Dispose();
            }
     
        }
    }
    Je l'ai appelé dans mon service avec ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
                {
                    if (unc.NetUseWithCredentials(SourcePath, SourceLogin, SourceDomain, SourcePassword))
                    {
                        //Code that accesses the remote resource
                    }
                    else
                    {
                        Library.WriteErrorLog("Impossible de se connecter à : " + SourcePath);
                    }
    Cela semble fonctionner sur un dossier partagé de mon réseau en mettant WORKGROUP pour le domaine, mais je me pose la question de l'utilisation sur un dossier Source sur le même PC que le service. Dans ce cas je laisse SourceLogin,SourceDomain et SourcePassword vides? C'est correct niveau codage?

    @+

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [PowerShell] Copie de fichier sur serveur distant
    Par talkk dans le forum Scripts/Batch
    Réponses: 1
    Dernier message: 08/12/2014, 09h16
  2. [AC-2007] Copie de fichiers et dossiers vers un dossier destination
    Par lio33 dans le forum VBA Access
    Réponses: 1
    Dernier message: 23/07/2013, 21h25
  3. Réponses: 1
    Dernier message: 30/10/2008, 17h42
  4. Copie de fichier accés refusé
    Par koKoTis dans le forum VBScript
    Réponses: 6
    Dernier message: 17/01/2008, 09h54
  5. Copie de fichier vers dossier
    Par crashyear dans le forum Langage
    Réponses: 1
    Dernier message: 26/04/2007, 21h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo