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();
}
}
}
}
1 Kommentare zum Snippet