2017-01-16 107 views
0

好吧我正在做一些應該比較簡單的事情,我相信我在這裏忽略了一些東西。C#流不斷跳過第一行

好吧我使用HttpWebRequest和WebResponse來檢測服務器上是否存在Robots.txt(並且工作正常)。但是,我想添加到做myList.Add(reader.ReadLine());哪(作品)。但問題是,它一直在跳過第一行。

https://www.assetstore.unity3d.com/robots.txt <這是我開始注意到這個問題的人(只是讓你知道我在說什麼)。它僅用於測試目的。 (看看這個鏈接,這樣你就可以知道我在說什麼)。

Anywho,它也沒有添加reader.ReadLine到我的列表(僅限第一行)。所以我並不完全瞭解發生了什麼事,我試圖查找這個,我發現的唯一的事情是故意要跳過一條線,我不想這樣做。

我的代碼如下。

Console.WriteLine("Robots.txt Found: Presenting Rules in (Robot Rules)."); 
       HttpWebRequest getResults = (HttpWebRequest)WebRequest.Create(ur + "robots.txt"); 
       WebResponse getResponse = getResults.GetResponse(); 
       using (StreamReader reader = new StreamReader(getResponse.GetResponseStream())) { 
        string line = reader.ReadLine(); 
        while(line != null && line != "#") { 
         line = reader.ReadLine(); 
         rslList.Add(line); 
         results.Text = results.Text + line + Environment.NewLine; // At first I thought it might have been this (nope). 
        } 


        // This didn't work either (figured perhaps maybe it was skipping because I had to many things. 
        // So I just put into a for loop, - nope still skips first line. 
        // for(int i = 0; i < rslList.Count; i++) { 
        //  results.Text = results.Text + rslList[i] + Environment.NewLine; 
        // } 
       } 
       // Close the connection sense it is no longer needed. 
       getResponse.Close(); 
       // Now check for user-rights. 
       CheckUserRights(); 

結果圖片。 enter image description here

+4

你扔掉第一'串線= reader.ReadLine();' - 改變你的'while'到'do' /' while'。 – Enigmativity

+0

謝謝你現在就試試。 – n1warhead

+0

謝謝你完全工作的人! – n1warhead

回答

1

變化時,下次調用read線

var line = reader.ReadLine(); //Read first line 
while(line != null && line != "#") { //while line condition satisfied 
    //perform your desired actions 
    rslList.Add(line); 
    results.Text = results.Text + line + Environment.NewLine; 
    line = reader.ReadLine(); //read the next line 
} 
+0

嘿謝謝你的幫助,我試着Enigmativity的第一個(因爲我第一次注意到它)和他的工作......謝謝你的時間。 – n1warhead

+0

@ n1warhead - 給Nkosi打勾 - 他花時間實施我的建議 - 爲我節省了時間。 :-) – Enigmativity