-1

我已经在这个简单的程序上工作了很长一段时间,但我似乎无法弄清楚我做错了什么。我正在尝试编写一个程序来编写第 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 语句失败后,是否有任何可能的方法来丢弃字符串?

4

2 回答 2

2

你只是从这个链接这个例子中阅读了一行注释:

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.

在示例列表中将包含所有行。

于 2013-07-25T19:50:05.627 回答
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);
            }
        }
于 2013-07-25T20:04:34.593 回答