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?

C#
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.
11,282 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 119.7K 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];  
    }  
    

Your answer

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