2017-01-16 9 views
0

これはかなり簡単なことですが、ここで何か見落としていると思います。C#ストリームは最初の行をスキップし続ける

いいえ、私はHttpWebRequestとWebResponseを使ってRobots.txtがサーバー上に存在するかどうかを検出します(それは完璧に機能します)。しかし、私はmyList.Add(reader.ReadLine())を行うために追加しようとしています。どちらが効果的かしかし、問題は、最初の行を飛ばし続けていることです。

https://www.assetstore.unity3d.com/robots.txt <これは私が問題に気付き始めた(ちょうど私が話していることを知っている)。これはテスト目的のためのものです。 (そのリンクを見れば、私が話していることについてのアイデアを得ることができます)。

誰でも、読者を私のリストに追加することはありません(最初の行のみ)。だから私は何が起こっているのか正確に理解していない、私はこれを見てみました、私が意図して行を飛ばしたいと思っている唯一のものは、私はそれをしたくありません。

マイコード以下。

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

読み取りラインを呼び出すの変更;' - 'do' /'にあなたの 'while'を変更while'。 – Enigmativity

+0

今すぐお試しください。 – n1warhead

+0

ありがとうございました。 – n1warhead

答えて

1

次のあなたはあなたが最初の `の文字列ライン= reader.ReadLineを()離れて投げている

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

関連する問題