2017-07-28 8 views
0

Je suis vraiment novice en matière de codage. J'ai donc du mal à obtenir une réponse à ma question directement. J'ai essayé de trouver une réponse à ma question de plusieurs façons (YouTube, stack overflow, google) mais je n'ai pas réussi à faire fonctionner mon programme correctement. Ce dont mon programme a besoin est d'obtenir une valeur d'un fichier m3u dans la cellule appropriée de ma table de données et de ne pas lire et ajouter absolument tout. Ce que j'ai trouvé en ligne est principalement comment lire text/csv/excel et importer toutes les données du fichier lui-même, ce n'est pas ce dont j'ai vraiment besoin ou code que je ne comprends pas comment mettre en œuvre pour mon utilisation, comme cette question: Reading from .txt file, then exporting data to DataGridView.Données de table définies par DataGridView C# qui reçoivent des données du fichier

J'ai défini des cellules qui devraient "aspirer" les données du fichier m3u.

Le fichier structure de fichier m3u est:

#EXTINF: -1 TVG-ID = "" TVG-name = "==== ==== Example1" TVG-logo = "" groupe -title = "", ==== Exemple1 ====
thestreamingsource1.com
#EXTINF: -1 tvg-ID = "" tvg-name = "==== Exemple2 ====" tvg- logo = "" group-title = "", ==== Exemple2 ====
thestreamingsource2.com
#EXTINF: -1 tvg-ID = "" tvg-name = "==== Exemple3 == == "tvg-logo =" "groupe-titre =" ", ==== Exemple3 ====
thestreamingsource3.com
#EXTINC: -1 tvg-ID = "" tvg-name = "==== Exemple4 ====" tvg-logo = "" groupe-titre = "", ==== Exemple4 ====
thestreamingsource4.com

Et je besoin du programme pour obtenir que les éléments suivants de la structure de valeur: TVG-ID (il est normal si elle est vide). tvg-name. tvg-logo (Ce n'est pas grave si c'est vide). titre de groupe. Jusqu'à présent, j'ai la chaîne qui lit tout le contenu du fichier et la grille de données prêt à accepter les données.

Le code derrière le formulaire est:

public class ThisClass 
{ 
    DataGridView my_datagridview = new DataGridView(); 
    DataTable my_datatable = new DataTable(); 

    // Constructor and other methods are in this class, 
    // but not showed here... 

    private void btnRead_Click(object sender, EventArgs e) 
    { 
     // Some codes are hidden here... 

     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      string sFileName = openFileDialog1.FileName;    
      string[] alltext = File.ReadAllLines(sFileName); 
      foreach (string text_line in alltext) 
      { 
       // MessageBox.Show(text_line); 
      } 
     } 
    } 
} 

Et la forme ressemble à ça: Read M3u UI

Je suis désolé si la question est déjà répondu, mais je ne pouvais pas trouver un Solution.

Heureux si vous pouviez aider.

Merci.

+0

S'il vous plaît donner un exemple de travail minimal et reformuler votre question avec cela. Et s'il vous plaît fournir toutes les informations directement dans la question (aucun lien ne devrait être nécessaire pour être cliqué pour répondre à la question). – Jan

+0

Merci travail à ce sujet –

+0

duplication possible de [Mettre un fichier .txt dans un DataGridView] (https://stackoverflow.com/questions/7095359/putting-a-txt-file-into-a-datagridview) – Ramankingdom

Répondre

0

Cela devrait vous y aller:

using System; 
using System.ComponentModel; 
using System.Drawing; 
using System.Windows.Forms; 
using System.IO; 

using System.Text.RegularExpressions; 

namespace DataGridView_45378237 
{ 


    public partial class Form1 : Form 
    { 

     DataGridView my_datagridview = new DataGridView();//the DataGridView which will be put on the form 
     BindingList<MyDatagridviewEntry> myDataGridviewSource = new BindingList<MyDatagridviewEntry>();//the BindingList from which the DataGridView will pull its data 


     public Form1() 
     { 
      InitializeComponent(); 
      InitializeDataGridView();//set the initial settings of the DataGridView 
     } 

     private void InitializeDataGridView() 
     { 
      my_datagridview.Location = new Point(this.Location.X + 15, this.Location.Y + 15);//define where to place it in the form(you could obviously just place one directly where you want using the wysiwyg) 
      this.Controls.Add(my_datagridview); 

      my_datagridview.AutoSize = true; 
      my_datagridview.AutoGenerateColumns = true; 
      my_datagridview.DataSource = myDataGridviewSource;//link the DataGridView with the BindingSource 

     } 

     private void btnRead_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
      openFileDialog1.InitialDirectory = @"C:\"; 
      openFileDialog1.Title = "Browse Text Files"; 

      openFileDialog1.CheckFileExists = true; 
      openFileDialog1.CheckPathExists = true; 

      openFileDialog1.DefaultExt = "m3u"; 
      openFileDialog1.Filter = "All files (*.*)|*.*|m3u files (*.m3u)|*.m3u"; 
      openFileDialog1.FilterIndex = 2; 
      openFileDialog1.RestoreDirectory = true; 

      openFileDialog1.ReadOnlyChecked = true; 
      openFileDialog1.ShowReadOnly = true; 

      if (openFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       string sFileName = openFileDialog1.FileName; 
       FillDataGridFromFile(sFileName);//send the file to get parsed 
      } 
     } 

     private void FillDataGridFromFile(string incomingFilePath) 
     { 
      //empty the list 
      myDataGridviewSource.Clear();//you may or may not want this... I don't know your full requirements... 

      //fill the list 
      using (StreamReader sr = new StreamReader(incomingFilePath)) 
      { 
       string currentLine = string.Empty; 

       while ((currentLine = sr.ReadLine()) != null) 
       { 
        /*This is not how I would write the production code, 
        * but for the sake of this example, this format works well 
        so that you know what is being done and why.*/ 
        string[] splittedString = currentLine.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 
        string f1 = splittedString.Length > 0 ? splittedString[0] : string.Empty;//if splittedString has more than 0 entries, the use entry[0], else use string.empty 
        string f2 = splittedString.Length > 1 ? splittedString[1] : string.Empty;//if splittedString has more than 1 entries, the use entry[1], else use string.empty 
        string f3 = GetTVGNameFromString(splittedString[0]);//Extract the text from within the string 
        string f4 = splittedString.Length > 3 ? splittedString[3] : string.Empty;//if splittedString has more than 3 entries, the use entry[3], else use string.empty 
        /**/ 

        //add the entry to the BindingSource 
        myDataGridviewSource.Add(new MyDatagridviewEntry { Col1 = f1, Col2 = f2, Col3 = f3, Col4 = f4 }); 
       } 
      } 
     } 



     private string GetTVGNameFromString(string incomingString) 
     { 
      string retval = string.Empty; 
      Regex rgx = new Regex("tvg-name=\"([^\"]*)\"");//use a grouping regex to find what you are looking for 
      if (rgx.IsMatch(incomingString)) 
      { 
       return rgx.Matches(incomingString)[0].Groups[1].Value; 
      } 
      return retval; 
     } 
    } 



    public class MyDatagridviewEntry 
    { 
     public string Col1 { get; set; } 
     public string Col2 { get; set; } 
     public string Col3 { get; set; } 
     public string Col4 { get; set; } 
    } 
}