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

Windows Forms Discussion :

Enregistrer plusieurs fichiers sans SaveFileDialog


Sujet :

Windows Forms

  1. #1
    Membre à l'essai
    Femme Profil pro
    Étudiant
    Inscrit en
    Octobre 2012
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2012
    Messages : 14
    Points : 11
    Points
    11
    Par défaut Enregistrer plusieurs fichiers sans SaveFileDialog
    Bonjour,

    Je reviens encore vers vous car j'ai besoin d'aide encore et parce que j'ai jamais été déçu de ce site

    Donc au départ il fallait que je créé un fichier vCard avec des contact (tout ça ca marche pas de problème), mais on à reçu le téléphone ce matin et sur celui-ci il ne gère pas un répertoire dans un seul .vcf, il faut que je l'ai créé un par un

    Je vais donc transformer mon code pour juste ouvrir éventuellement une seule fois la boîte de dialogue pour choisir le répertoire (donc le chemin ou chaque vCard sera enregistrer) mais là et le problème comment faire :/

    Voici mon code actuel :

    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
     
    List<Contact> lesContacts;
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void btn_créer_Click(object sender, EventArgs e)
            {
                lesContacts = UtilityVCF.CreateListContacts(rtb_vcf);
     
                string fichierVCF = UtilityVCF.TransformersVCF(lesContacts);
     
                if (fichierVCF == "Error")
                {
                    MessageBox.Show("Il n'y a aucun contact");
                }
                else
                {
                    saveFileDialogVCF.Filter = "Fichier vCard *.vcf|*.vcf";
     
                    if (saveFileDialogVCF.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        StreamWriter sw = new StreamWriter(saveFileDialogVCF.FileName);
                        sw.AutoFlush = true;
                        sw.Write(fichierVCF);
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
    Et voici mes fonctions static que j'utilise au dessus :

    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
     
            public static string TransformersVCF(List<Contact> listeContacts)
            {
                string stringVCF = "";
     
                if (listeContacts.Count < 0)
                {
                    stringVCF = "Error";
                }
                else
                {
                    foreach (Contact leContact in listeContacts)
                    {
                        stringVCF = stringVCF + "BEGIN:VCARD"
                            + Environment.NewLine
                            + "VERSION:2.1"
                            + Environment.NewLine
                            + "N:" + leContact._Nom + ";" + leContact._Prenom
                            + Environment.NewLine
                            + "FN:" + leContact._Prenom + " " + leContact._Nom
                            + Environment.NewLine
                            + "TEL;CELL:" + leContact._NumTel
                            + Environment.NewLine
                            + "END:VCARD"
                            + Environment.NewLine;
                    }
                }
     
                return stringVCF;
            }
     
            public static List<Contact> CreateListContacts(RichTextBox rtbContacts)
            {
                List<Contact> Contacts = new List<Contact>();
     
                foreach (string ligne in rtbContacts.Lines)
                {
                    string[] ligneContact = ligne.Split(new Char[] { ' ', '\t' });
                    string[] leContact = new string[3];
                    int attribut = 0;
                    foreach (string mot in ligneContact)
                    {
                        if (mot != " ")
                        {
                            leContact[attribut] = mot;
                            attribut += 1;
                        }
                    }
                    Contact unContact = new Contact(leContact[0], leContact[1], leContact[2]);
                    Contacts.Add(unContact);
                }
     
                return Contacts;
            }
    et voici ma classe contact (au cas ou ):

    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
     
        class Contact
        {
            #region Attribut(s)
     
            private string Nom;
            private string Prenom;
            private string NumTel;
     
            #endregion
     
            #region Accesseur(s)
     
            public string _Nom
            {
                get { return Nom;}
            }
     
            public string _Prenom
            {
                get { return Prenom; }
            }
     
            public string _NumTel
            {
                get { return NumTel; }
            }
     
            #endregion
     
            #region Construteur(s)
     
            public Contact(string nom, string prenom, string numTel)
            {
                Nom = nom;
                Prenom = prenom;
                NumTel = numTel;
            }
     
            #endregion
        }
    Merci d'avance, si il manque des précision demandé moi

    Merci d'avance

    Cordialement

    Foudogue

  2. #2
    Membre du Club
    Inscrit en
    Février 2012
    Messages
    37
    Détails du profil
    Informations forums :
    Inscription : Février 2012
    Messages : 37
    Points : 55
    Points
    55
    Par défaut
    salut,

    dans un premier temps, tu peux surcharger ta fonction TransformersVCF pour qu'elle ne prenne plus une liste de contact mais un seul en paramètres.

    puis dans ta procédure btn_créer_Click, tu parcours ta liste de contact avec un for each et tu écris un fichier par contact

    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
     
    private void btn_créer_Click(object sender, EventArgs e)
    {
    	StreamWriter sw;
    	lesContacts = UtilityVCF.CreateListContacts(rtb_vcf);
     
    	string myFolder = "";
    	int iCount = 1;
     
    	if (FolderBrowserDialogVCF.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    	{
    		myFolder = FolderBrowserDialogVCF.SelectedPath;
     
    		foreach (Contact leContact in lesContacts)
    		{
    			string fichierVCF = UtilityVCF.TransformersVCF(leContact);
    			sw = = new StreamWriter( myFolder + "vCard" + iCount.ToString() + ".vcf");
    			sw.AutoFlush = true;
    			sw.Write(fichierVCF);
    			sw.Flush();
    			sw.Close();
    			sw.Dispose();
    			iCount++;
    		}
    	}
    }

Discussions similaires

  1. Impossible d'enregistrer un fichier sans qu'excel plante!
    Par Lounalou dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 26/04/2013, 08h13
  2. Réponses: 9
    Dernier message: 04/02/2013, 13h45
  3. php - enregistrer plusieurs fichiers pdf dans un fichier pdf
    Par bella1 dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 1
    Dernier message: 25/12/2011, 22h20
  4. [XL-2003] Par Macro, enregistrer le fichier sans les Macros
    Par malabarbe dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 07/01/2011, 11h22
  5. [VBA-E] Enregistrer un fichier sans sa macro
    Par KKshi666 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 19/04/2007, 16h11

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