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

Contribuez .NET Discussion :

[C#2.0] Comment écrire dans un fichier sur un serveur ?


Sujet :

Contribuez .NET

  1. #1
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut [C#2.0] Comment écrire dans un fichier sur un serveur ?
    Bonjour,

    Environnement: VS .NET 2005 Fr
    Framework: 2.0
    OS: Windows XP

    J'ai besoin d'effectuer une sauvegarde sur un serveur, je dois donc créer un fichier sur un autre pc.
    Je ne sais pas comment écrire path dans
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    FileStream fs = new FileStream(path, FileMode.Create);
    Si le chemin est C:\\temp\\svg.txt, a quel endroit je dois rajouter l'IP?
    Merci

  2. #2
    Inscrit Avatar de bilb0t
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    378
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2003
    Messages : 378
    Points : 283
    Points
    283
    Par défaut
    Le problème c'est que si ton C n'est pas partagé sur le serveur tu va pouvoir dancer sur ta tête pour aller y mettre une info.

    Maintenant si je cherche, je crois que j'ai une classe qui permet de faire un mapping du style: x: --> \\192.168.0.2\zonepartagée. Si ça t'intéresse.

  3. #3
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    pour etre tout a fait honnete je n'ai pas bien compris ta reponse...

    tu veux dire que si je mets :
    \\192.168.0.2\\C:\\temp\\svg.txt

    et que le C est partagé sur l'autre ordi ca marchera?

    Citation Envoyé par meli0207
    pour etre tout a fait honnete je n'ai pas bien compris ta reponse...

    tu veux dire que si je mets :
    \\192.168.0.2\\C:\\temp\\svg.txt

    et que le C est partagé sur l'autre ordi ca marchera?
    Je viens de tester ca ne marche pas, j'obtient l'exception suivante :
    The given path's format is not supported.

    En fait il suffit de connecter un lecteur distant dans le panneau de configuration, par exemple on crée Z:


    pour path on prend alors Z:\\svg.txt

  4. #4
    Inscrit Avatar de bilb0t
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    378
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2003
    Messages : 378
    Points : 283
    Points
    283
    Par défaut
    c'est se que je te disais.

    J'ai une classe qui permet de te connecter à un serveur distant et donc de faire automatiquement ce que tu fais manuellement. c'est tout.

  5. #5
    Membre habitué Avatar de meli0207
    Inscrit en
    Novembre 2004
    Messages
    208
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Novembre 2004
    Messages : 208
    Points : 133
    Points
    133
    Par défaut
    ah ok. pourrais-tu me l'envoyer?
    merci!!

  6. #6
    Inscrit Avatar de bilb0t
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    378
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2003
    Messages : 378
    Points : 283
    Points
    283
    Par défaut
    Y a qu'à demander.

    Et pour info c'est pas moi qui ai écrit la classe. Toutes les infos sont ds le "header".

    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
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
     
    /*======================================================
    AejW.com - Network Drives
    -------------------------
    Build:             0015
    Author:            Adam ej Woods
    Modified:          14/05/2004
    Ownership:         Copyright (c)2004 Adam ej Woods
    Source:            http://www.aejw.com/
    EULA:              In no way can this class be disturbed without my
     permission, this means reposting on a web site, cdrom, or any other form
     of media. The code can be used for commercial or personal purposes, as
     long as credit is given to the author. The header (this information) can
     not be modified or removed. www.CodeProject.com has permission to
     disturbe this class.
    =======================================================*/
    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
     
    namespace aejw.Network
    {
        /// <summary>
        /// AejW.com - Network Drive Interface
        /// </summary>
        public class NetworkDrive{
     
            #region API
            [DllImport("mpr.dll")] private static extern int WNetAddConnection2A
    (ref structNetResource pstNetRes, string psPassword, string psUsername,
     int piFlags);
            [DllImport("mpr.dll")] private static extern int
     WNetCancelConnection2A(string psName, int piFlags, int pfForce);
            [DllImport("mpr.dll")] private static extern int WNetConnectionDialog
    (int phWnd, int piType);
            [DllImport("mpr.dll")] private static extern int WNetDisconnectDialog
    (int phWnd, int piType);
            [DllImport("mpr.dll")] private static extern int
     WNetRestoreConnectionW(int phWnd, string psLocalDrive);
     
            [StructLayout(LayoutKind.Sequential)]
            private struct structNetResource{
                public int iScope;
                public int iType;
                public int iDisplayType;
                public int iUsage;
                public string sLocalName;
                public string sRemoteName;
                public string sComment;
                public string sProvider;
            }
     
            private const int RESOURCETYPE_DISK = 0x1;
     
            //Standard    
            private const int CONNECT_INTERACTIVE = 0x00000008;
            private const int CONNECT_PROMPT = 0x00000010;
            private const int CONNECT_UPDATE_PROFILE = 0x00000001;
            //IE4+
            private const int CONNECT_REDIRECT = 0x00000080;
            //NT5 only
            private const int CONNECT_COMMANDLINE = 0x00000800;
            private const int CONNECT_CMD_SAVECRED = 0x00001000;
     
            #endregion
     
            #region Propertys and options
            private bool lf_SaveCredentials = false;
            /// <summary>
            /// Option to save credentials are reconnection...
            /// </summary>
            public bool SaveCredentials{
                get{return(lf_SaveCredentials);}
                set{lf_SaveCredentials=value;}
            }
            private bool lf_Persistent = false;
            /// <summary>
            /// Option to reconnect drive after log off / reboot ...
            /// </summary>
            public bool Persistent{
                get{return(lf_Persistent);}
                set{lf_Persistent=value;}
            }
            private bool lf_Force = false;
            /// <summary>
            /// Option to force connection if drive is already mapped...
            /// or force disconnection if network path is not responding...
            /// </summary>
            public bool Force{
                get{return(lf_Force);}
                set{lf_Force=value;}
            }
            private bool ls_PromptForCredentials = false;
            /// <summary>
            /// Option to prompt for user credintals when mapping a drive
            /// </summary>
            public bool PromptForCredentials{
                get{return(ls_PromptForCredentials);}
                set{ls_PromptForCredentials=value;}
            }
     
            private string ls_Drive = "s:";
            /// <summary>
            /// Drive to be used in mapping / unmapping...
            /// </summary>
            public string LocalDrive{
                get{return(ls_Drive);}
                set{
                    if(value.Length>=1){
                        ls_Drive=value.Substring(0,1)+":";
                    }else{
                        ls_Drive="";
                    }
                }
            }
            private string ls_ShareName = "\\\\Computer\\C$";
            /// <summary>
            /// Share address to map drive to.
            /// </summary>
            public string ShareName{
                get{return(ls_ShareName);}
                set{ls_ShareName=value;}
            }
            #endregion
     
            #region Function mapping
            /// <summary>
            /// Map network drive
            /// </summary>
            public void MapDrive(){zMapDrive(null, null);}
            /// <summary>
            /// Map network drive (using supplied Password)
            /// </summary>
            public void MapDrive(string Password){zMapDrive(null, Password);}
            /// <summary>
            /// Map network drive (using supplied Username and Password)
            /// </summary>
            public void MapDrive(string Username, string Password){zMapDrive
    (Username, Password);}
            /// <summary>
            /// Unmap network drive
            /// </summary>
            public void UnMapDrive(){zUnMapDrive(this.lf_Force);}
            /// <summary>
            /// Check / restore persistent network drive
            /// </summary>
            public void RestoreDrives(){zRestoreDrive();}
            /// <summary>
            /// Display windows dialog for mapping a network drive
            /// </summary>
            public void ShowConnectDialog(Form ParentForm){zDisplayDialog
    (ParentForm,1);}
            /// <summary>
            /// Display windows dialog for disconnecting a network drive
            /// </summary>
            public void ShowDisconnectDialog(Form ParentForm){zDisplayDialog
    (ParentForm,2);}
     
     
            #endregion
     
            #region Core functions
     
            // Map network drive
            private void zMapDrive(string psUsername, string psPassword){
                //create struct data
                structNetResource stNetRes = new structNetResource();
                stNetRes.iScope=2;
                stNetRes.iType=RESOURCETYPE_DISK;
                stNetRes.iDisplayType=3;
                stNetRes.iUsage=1;
                stNetRes.sRemoteName=ls_ShareName;
                stNetRes.sLocalName=ls_Drive;            
                //prepare params
                int iFlags=0;
                if(lf_SaveCredentials){iFlags+=CONNECT_CMD_SAVECRED;}
                if(lf_Persistent){iFlags+=CONNECT_UPDATE_PROFILE;}
                if(ls_PromptForCredentials)
                  {iFlags+=CONNECT_INTERACTIVE+CONNECT_PROMPT;}
                if(psUsername==""){psUsername=null;}
                if(psPassword==""){psPassword=null;}
                //if force, unmap ready for new connection
                if(lf_Force){try{zUnMapDrive(true);}catch{}}
                //call and return
                int i = WNetAddConnection2A(ref stNetRes, psPassword,
     psUsername, iFlags);            
                if(i>0)
                 {throw new System.ComponentModel.Win32Exception(i);}
     
            }
     
            // Unmap network drive
            private void zUnMapDrive(bool pfForce){
                //call unmap and return
                int iFlags=0;
                if(lf_Persistent){iFlags+=CONNECT_UPDATE_PROFILE;}
                int i = WNetCancelConnection2A(ls_Drive, iFlags, Convert.ToInt32
    (pfForce));
                if(i>0){throw new System.ComponentModel.Win32Exception(i);}
            }
            // Check / Restore a network drive
            private void zRestoreDrive(){
                //call restore and return
                int i = WNetRestoreConnectionW(0, null);
                if(i>0){throw new System.ComponentModel.Win32Exception(i);}
            }
            // Display windows dialog
            private void zDisplayDialog(Form poParentForm, int piDialog)
            {
                int i = -1;
                int iHandle = 0;
                //get parent handle
                if(poParentForm!=null){
                    iHandle = poParentForm.Handle.ToInt32();
                }
                //show dialog
                if(piDialog==1){
                    i = WNetConnectionDialog(iHandle, RESOURCETYPE_DISK);
                }else if(piDialog==2){
                    i = WNetDisconnectDialog(iHandle, RESOURCETYPE_DISK);
                }
                if(i>0){throw new System.ComponentModel.Win32Exception(i);}
                //set focus on parent form
                poParentForm.BringToFront();
            }
     
     
            #endregion
     
        }
     
    }

  7. #7
    Expert éminent
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 64
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Points : 7 879
    Points
    7 879
    Par défaut
    Merci de ta contribution bilbot, pour la peine je déplace ce sujet là où quelques rédacteurs vont se pencher sur ta réponse

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

Discussions similaires

  1. comment écrire dans un fichier avec Ant-apache
    Par sidi.elmoctar dans le forum XML/XSL et SOAP
    Réponses: 1
    Dernier message: 25/04/2006, 17h44
  2. [Delphi] Comment écrire dans un fichier texte ?
    Par Mickey.jet dans le forum Débuter
    Réponses: 2
    Dernier message: 01/04/2006, 14h00
  3. [C#] Comment écrire dans un fichier ?
    Par guillaume16 dans le forum C#
    Réponses: 9
    Dernier message: 30/03/2006, 13h39
  4. [JAR] Comment écrire dans un fichier properties dans un jar
    Par Alec6 dans le forum Entrée/Sortie
    Réponses: 3
    Dernier message: 05/10/2004, 15h16

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