tags:

views:

2458

answers:

5

I'd like to create an array from a CSV file.

This is about as simple as you can imagine, the CSV file will only ever have one line and these values:

Device, SignalStrength, Location, Time, Age.

I'd like to put these values into one dimensional array.

I've tried some examples but they've all been more complicated than required.

+15  A: 

If there is only ever one line then do something like this:

using System;
using System.IO;

class Program
{
    static void Main()
    {
     String[] values = File.ReadAllText(@"d:\test.csv").Split(',');
    }
}
Andrew Hare
thanks
DNN
No, no, no this is way too complex!!
ChaosPandion
+7  A: 

You can try the some thing like the below LINQ snippet.

string[] allLines = File.ReadAllLines(@"E:\Temp\data.csv");

    var query = from line in allLines
                let data = line.Split(',')
                select new
                {
                    Device = data[0],
                    SignalStrength = data[1],
                    Location = data[2], 
                    Time = data[3],
                    Age = Convert.ToInt16(data[4])
                };
Ramesh
A: 

Have a look at Marcos Meli's open-source FileHelpers for .NET. The Quick-Start Delimited sample should do pretty much what you want, and the framework is quite useful for other scenarios as well.

mdb
A: 

I had this one bookmarked; I think I've used it before at work, but I'm not positive. Anyway, it's worth trying.

http://www.codeproject.com/KB/database/CsvReader.aspx

TrueWill
A: 

Check out the Csv reader/writer in CommonLibrary.NET

It's pretty easy to use.

james