2013-07-25 23 views
-1

我一直在這個簡單的程序上玩了很長一段時間,我似乎無法弄清楚我做錯了什麼。我試圖編寫一個程序來寫出第21個字符是'A'的每一行。問題是,當它執行時它只會讀取第一條記錄,並不斷重複該記錄。這裏是我的代碼:StreamReader重複相同的字符串

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語句?

+0

並移動您轉發該代碼的一部分?因爲,對我而言,似乎在每一個循環中,你都開始從頭開始閱讀這些行。 – Storm

回答

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. 

在這個例子名單將持有的所有線路。

+0

是的,這有幫助。非常感謝你。 –

+0

偉大請標記爲正確的我需要的點:) – Bit

0

可以使用StreamReader.Peek()方法在一個while循環,看看是否有任何更多的行/字符閱讀:

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); 
      } 
     } 
+0

這仍然不會走到下一行。你需要While(sr.ReadLine())使它移動到下一行。 – Bit

+0

我測試過,它的工作原理。 Peek()返回下一個可用字符,但不會消耗它。 ReadLine()讀取該行並同時使流中的指針前進。測試它。 – Dylan

+0

恰好你不會在循環中前進,它只會測試是否存在另一條線。 – Bit