StreamReader重复相同的字符串

时间:2013-07-25 19:44:51

标签: c# streamreader

我一直在研究这个简单的程序已经有一段时间了,我似乎无法弄清楚我做错了什么。我试图让一个程序写出第21个字符是'A'的每一行。问题是,当它执行时,它只会读取第一条记录,并且只是不断重复该记录。这是我的代码:

    private void Send(string BaleLine)
    {
        //Setting Stop to false so buttons will work after Stop has been pushed.
        GlobalVariables.Stop = false;
        String Lines;
        String BaleChecker;
        int Counter = 0;

        //File location we are reading from.
        String File = @"C:\Temp\Forte.dat";

        //Creating the new Stream Reader, to read one line at a time.
        System.IO.StreamReader FileReader = new System.IO.StreamReader(File);

        //Writing until all the files are gone.
        if (ApplicationPort.IsOpen == false)
        {
            //Opening port.
            ApplicationPort.Open();
        }
        do
        {
            if (GlobalVariables.Stop == false)
            {
                Lines = FileReader.ReadLine();
                Application.DoEvents();
                //Checking the bale line of the data.
                BaleChecker = Lines.Substring(21, 1);

                if (BaleChecker == BaleLine)
                {
                    //Writing the data to the text box..
                    TextBox1.AppendText(Environment.NewLine + Lines);

                    //Writing the strings to the Application Port.
                    ApplicationPort.Write(Lines);

                    Counter++;

                    //Giving the Forte Data Gatherer a break.
                    if (Counter == 5)
                    {
                        System.Threading.Thread.Sleep(3000);
                        Counter = 0;
                    }
                }
            }
            else
            {
                //Closing the port before leaving the method.
                ApplicationPort.Close();
                return;
            }
        }
        while (Lines != null);

        //Closing the comm port after the writing is finished.
        ApplicationPort.Close();

        //Success message saying that everyhting is written to the comm port.
        TextBox1.AppendText(Environment.NewLine + "All files successfully written to the serial port.");
    }

    private void SendALinebtn_Click(object sender, EventArgs e)
    {
        Send("A");
    }

也许是否有任何可能的方法在If语句失败后丢弃字符串?

2 个答案:

答案 0 :(得分:2)

您只是从此link此示例中读取一行注释:

Program that reads all lines: C#

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
    //
    // Read in a file line-by-line, and store it all in a List.
    //
    List<string> list = new List<string>();
    using (StreamReader reader = new StreamReader("file.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
        list.Add(line); // Add to list.
        Console.WriteLine(line); // Write to console.
        }
    }
    }
}

Output

First line of your file.txt file.
Second line.
Third line.
Last line.

在示例列表中将保留所有行。

答案 1 :(得分:0)

您可以在while循环中使用StreamReader.Peek()方法来查看是否还有更多行/字符要读取:

using (StreamReader sr = new StreamReader(@"C:\Temp\Atextfile.txt"))
       {
            while(sr.Peek() > 0) // check if there is more to read
            {
                string line = sr.ReadLine(); // advance in stream
                Console.WriteLine(line);
            }
        }