Advertisement
Rakshalpha

CSVReader

Sep 25th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Windows.Forms;
  6.  
  7. namespace CSV_Reader
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         List<string> listFiles = new List<string>();
  12.  
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private void Button1_Click(object sender, EventArgs e)
  19.         {
  20.             OpenDirectory();
  21.         }
  22.        
  23.         private void OpenDirectory()
  24.         {
  25.             listFiles.Clear();
  26.             listView.Items.Clear();
  27.             using (FolderBrowserDialog fbd = new FolderBrowserDialog()
  28.             {
  29.                 Description = "Select your path."
  30.             })
  31.             {
  32.                 if (fbd.ShowDialog() == DialogResult.OK)
  33.                 {
  34.                     txtPath.Text = fbd.SelectedPath;
  35.                     foreach (string item in Directory.GetFiles(fbd.SelectedPath))
  36.                     {
  37.                         FileInfo fi = new FileInfo(item);
  38.                         if (fi.FullName.Contains(".xlsx"))
  39.                         {
  40.                             imageList1.Images.Add(Icon.ExtractAssociatedIcon(item));
  41.                             listFiles.Add(fi.FullName);
  42.                             listView.Items.Add(fi.Name, imageList1.Images.Count - 1);
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.  
  49.         private void ListView_SelectedIndexChanged(object sender, EventArgs e)
  50.         {
  51.             if (listView.FocusedItem != null)
  52.             {
  53.                 ReadAllData(listFiles[listView.FocusedItem.Index]);
  54.             }
  55.         }
  56.  
  57.         void ReadAllData(string fileToRead)
  58.         {
  59.             using (var reader = new StreamReader(fileToRead))
  60.             {
  61.                 List<string> listA = new List<string>();
  62.                 List<string> listB = new List<string>();
  63.                 while (!reader.EndOfStream)
  64.                 {
  65.                     var line = reader.ReadLine();
  66.                     var values = line.Split(',');
  67.  
  68.                     foreach (string item in values)
  69.                     {
  70.                         listA.Add(item);
  71.                     }
  72.                 }
  73.                 DisplayData(listA);
  74.             }
  75.         }
  76.  
  77.         void DisplayData(IList<string> list)
  78.         {
  79.             foreach (string item in list)
  80.             {
  81.                 MessageBox.Show(item);
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement