Wednesday, October 21, 2015

Read CSV

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(System.IO.File.OpenRead(@"C:\Users\Dilupa\Desktop\readings.csv"));
            System.IO.StreamWriter file = new System.IO.StreamWriter(System.IO.File.OpenWrite(@"C:\Users\Dilupa\Desktop\readingsnew.txt"));
            List<string> listA = new List<String>();
            List<string> listB = new List<String>();
            List<string> listC = new List<String>();
            List<string> listD = new List<String>();
            //string vara1, vara2, vara3, vara4;
while (!reader.EndOfStream)
    {
    string line = reader.ReadLine();
    if (!String.IsNullOrWhiteSpace(line))
        {
        string[] values = line.Split(',');
        if (values.Length >= 3)
            {
            listA.Add(values[0]);
            listB.Add(values[1]);
            listC.Add(values[2]);
           // listD.Add(values[3]);
            }
        }
    }
string[] firstlistA = listA.ToArray();
string[] firstlistB = listB.ToArray();
string[] firstlistC = listC.ToArray();
//string[] firstlistD = listD.ToArray();
//textBox1.Text = firstlistA.Length.ToString();
//textBox1.Text = textBox1.Text + "\r\n" + firstlistB.Length.ToString();
//textBox1.Text = textBox1.Text + "\r\n" + firstlistC.Length.ToString();
//textBox1.Text = textBox1.Text + "\r\n" + firstlistD.Length.ToString();

    foreach (string s in firstlistB)
    {
        textBox2.Text = textBox2.Text + "\r\n" + s;
    }
    foreach (string s in firstlistC)
    {
        textBox3.Text = textBox3.Text + "\r\n" + s;
    }
    for (int i = 1; i < firstlistA.Length; i++)
    {
        textBox1.Text = textBox1.Text + "\r\n" + firstlistA[i];
        //file.Write(firstlistA[i-1] + ",");
        file.Write(string.Join(",", firstlistA[i-1]));
    }

}
        }
}