Feedback

C# - Read CSV and splitt data with ' , '

Veröffentlicht von am 11.11.2015
(1 Bewertungen)
This Code ask for a path to a CSV and splitt the data with a ' , '.

You need System.Text
System.IO

using System;
using System.Text;
using System.IO;

namespace Snippet1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please give a Path to read a CSV ");

            //Read a Path for a CSV
            string path = Console.ReadLine();
            readCSV(path);            
        }
        /// <summary>
        /// read CSV file
        /// split all lines with a ' , '
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string[][] readCSV(string path)
        {

            if (File.Exists(path))
            {

                string[] lines = File.ReadAllLines(path, Encoding.Default);

                string[][] result = new string[lines.Length][];

                //Split all lines with a ','
                for (int i = 0; i < lines.Length; i++)
                {
                    result[i] = lines[i].Split(',');
                }
                return result;
            }
            else
            {
                throw new FileNotFoundException();
            }  
        }
    }
}
Abgelegt unter readCSV, split.

1 Kommentare zum Snippet

Anonymous2 schrieb am 26.11.2015:
Das CSV Format erlaubt viel mehr Trennzeichen als nur ','.
 

Logge dich ein, um hier zu kommentieren!