Share via

Get Column Name with Index Using StreamReader

Winston TheFifth 106 Reputation points
2022-07-14T03:08:03.783+00:00

How do you get the column name given the column index using a streamreader?

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


1 answer

Sort by: Most helpful
  1. Viorel 126.9K Reputation points
    2022-07-14T04:51:40.653+00:00

    In simple cases, try something like this:

    int column_index = . . .  
    string column_name;  
      
    using( StreamReader sr = new StreamReader( "MyFile.csv"))  
    {  
       string line = sr.ReadLine( );  
       var a = line.Split( ",")  
       column_name = a[column_index];  
    }  
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.