Windows Phone Developers

Saturday, October 18, 2008

Insert CSV File to Array using C#

Read Text File to Array C#/ Insert CSV File to Array using C# / C# Array from Text/CSV Files

ReadAllLines method can be used to read the contents of a CSV file to array. Each line of the text file will become the elements of the array

public void ReadFile2Array()

{

String[] FileContent = File.ReadAllLines(@"C:\Temp\vba.csv");

int LineNo = 0;

foreach (string Line in FileContent)

{

LineNo +=1;

Console.WriteLine("Line " + LineNo.ToString() + " " + Line);

}

}

ReadAllLines method opens a text file, reads all lines of the file into a string array, and then closes the file.

The input file is shown below




The output will be like
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

1 comment:

  1. string file_name = "c:\\uploadme.txt";
    if (System.IO.File.Exists(file_name) == true)
    {
    String[] FileContent = File.ReadAllLines(file_name);

    string fileRow = "";
    int LineNo = 0;

    foreach (string Line in FileContent)
    {
    LineNo +=1;
    fileRow = fileRow + "Line " + LineNo.ToString() + " " + Line + "\r\n";
    richTextBox1.Text = fileRow;
    }

    }
    else
    {
    MessageBox.Show("No such file " + file_name);
    }

    ReplyDelete