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