2017-01-16 109 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