Lire un fichier text

sahlyhassani - 25 déc. 2012 à 10:44
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 - 25 déc. 2012 à 10:54
Bonjour je veut extracter les donees dans un fichier text :

fichier.txt

1|xxx|yyy|[m]
2|rrr|hhh|[f]

Comment passer du 1er etudiant au 2eme pour l ajouter dans la base de donnée

private void bOpenFileDialog_Click(object sender, RoutedEventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
 
            // Set filter options and filter index.
            openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
 
            openFileDialog1.Multiselect = true;
 
            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();
 
            // Process input if the user clicked OK.
            if (userClickedOK == true)
            {
                // Open the selected file to read.
                System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
 
                using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
                {
                    // Read the first line from the file and write it the textbox.
                    char[] splitchar = { ' ' };
                    string[] mots = reader.ReadToEnd().Split(splitchar);
                   // tbResults.Text = reader.ReadToEnd();
                    foreach (String mot in mots)
                        tbResults.Text ="\n"+ mot;
                    listBox1.ItemsSource = mots;
 
                    ServiceReference1.WebServiceAlerteSoapClient s = new ServiceReference1.WebServiceAlerteSoapClient();
                    ServiceReference1.etudiant a = new ServiceReference1.etudiant();
                    a.id=mots[0];
                    a.nom = mots[1];
                    a.prenom = mots[2];
                    a.sexe=mots[3];
 
                    s.AddEtudAsync(a);
 
 
                }
                fileStream.Close();
 
            }
        }

1 réponse

Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 656
25 déc. 2012 à 10:54
Bonjour, le code que tu utilises lit le fichier entier.
Regarde ce code montre comment lire ligne par ligne.


Whismeril
0
Rejoignez-nous