c#string list读取单个元素,Console.Read()问题

时间:2014-12-13 03:59:58

标签: string console-application

假设我有一个Test.csv文件

Screen Name,Tweet ID,Dates,Tweets
OilGasMalaysia,5.21E+17,10/13/2014,Dropping Oil Prices Send Shockwaves Through Energy Sector - http://t.co/2dne6NGwRE http://t.co/hRUXjEQGCz
OilBRK,5.22E+17,10/13/2014,Oil prices down in Asian trade as weak demand weighs: Oil prices fell in Asian trade Monday on growi... http://t.co/yuL4D0FPx7 #Oil #BRK
OilBRK,5.22E+17,10/13/2014,Oil prices down in Asian trade as weak demand weighs: SINGAPORE: Oil prices fell in Asian trade Mond... http://t.co/RNBIRiQCu3 #Oil #BRK
OilBRK,5.22E+17,10/13/2014,Oil price down again on growth fears: Global oil prices fall again with Brent crude at a near four-y... http://t.co/AHUrTYORK2 #Oil #BRK
OilBRK,5.22E+17,10/13/2014,Oil heads for four-year low on Saudi output signal: Brent crude oil fell below $88 a barrel on Monda... http://t.co/O4JUbdFQ5o #Oil #BRK

我有以下程序,我想使用StreamReader加载Test.csv

static void Main(string[] args)
    {

        List<string> CsvFile = new List<string>();

        int counter = 0;
        string line;

        // Read the file and display it line by line.
        System.IO.StreamReader file =
           new System.IO.StreamReader("Test.csv");
        while ((line = file.ReadLine()) != null)
        {
            CsvFile.Add(line);
            // Console.WriteLine(line);
            counter++;
            //Console.ReadLine();
        }

        file.Close();

        for (int i = 0; i < CsvFile.Count; i++)
        {
            Console.Write(CsvFile[i].ToString() + "\n");
            Console.Read();

        }

    }

每件事都很好,文件正在加载到内存中,但当它转到for循环时,每两个元素都显示在一行中,问题与Console.Read()有关。

问题: 当用户使用循环按键时,如何显示下一个单个元素

谢谢。

1 个答案:

答案 0 :(得分:1)

我猜你正在点击Enter,这实际上是两个字符 - 回车和换行。如果将Console.Read()更改为Console.ReadLine(),它将起作用。