1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C# read csv file row & columns values?

Discussion in 'C, C++, C#' started by sandrine10, Jun 16, 2014.

  1. sandrine10

    sandrine10 Power Member

    Joined:
    Apr 14, 2010
    Messages:
    621
    Likes Received:
    63
    Location:
    CyberLand
    Hi mates,

    Spent all the day searching how to read a csv file with no succes in practice, what i want do is getting rows and columns values using streamreader & increament statements:

    Example of the file:
    userid1;password1
    userid2;password2
    userid3;password3
    userid4;password4

    Anyone for help?!
     
  2. madworld

    madworld Newbie

    Joined:
    May 6, 2014
    Messages:
    14
    Likes Received:
    7
    Location:
    Up North
    Something like this perhaps?

    Code:
    var reader = new StreamReader(File.OpenRead("input.txt"));
    var data = new List<List<string>>();
            
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        var values = line.Split(';');
                
        data.Add(new List<String> { values[0], values[1] });
    }
    
    Console.WriteLine(data[0][0]); //  -> userid1
    Console.WriteLine(data[0][1]); //  -> password1
    
     
    • Thanks Thanks x 1
  3. sandrine10

    sandrine10 Power Member

    Joined:
    Apr 14, 2010
    Messages:
    621
    Likes Received:
    63
    Location:
    CyberLand
    I figured that out.