2017-03-23 3 views
1

SteamReaderに問題があります。文字列が存在することがわかっていても、私のStreamReaderはファイル内の特定の文字列一致を見つけられません。これを示す最善の方法はイメージを通してです。どのように動作するかを理解することができます。後でコードを提供します: enter image description hereStreamReaderがファイル内に文字列を見つけられません

Imageは、文字列がファイル内にあるかどうかを確認するコードを表示します。開いているテキスト文書は、チェックされたファイルです。これは、新しいフォーム(このフォーム)が情報を入力した後に作成されるときに、変数として渡されます。フォームボタンは、図のようにコードを実行するボタンです。文字列が見つからない場合は、メッセージボックスが表示されます。

ファイルがそれを拾うていないだけで、文字列をcontiansが、見ることができるように:/

私は、これは非常に単純なものである疑いがあるが、私は目の新鮮なペアを必要とします。

private void btnGetActivities_Click(object sender, EventArgs e) 
    { 
     if (File.Exists(sVenueName.ToString() + ".txt")) 
     { 
      using (StreamReader RetrieveEvents = new StreamReader(sVenueName.ToString() + ".txt"))     //Create a new file with the name of the username variable 
      { 
       string EventString; 
       string EventType; 
       string EventPeopleAttending; 
       string line = RetrieveEvents.ReadLine();          //Declare string variable to hold each line in the file 

       while (RetrieveEvents.Peek() != -1) 
       { 


        if (line.Contains("Event Name:")) //When this line is found, 
        { 
         EventString = line.Remove(0, 12);       //Remove the characters from the line and store it in a variable 
         lstDisplayActivities.Items.Add(line); 

         line.Skip(1).Take(2); 
         EventType = line.Remove(0, 12); 
         lstDisplayActivities.Items.Add(line); 

         line.Skip(2).Take(3); 
         EventPeopleAttending = line.Remove(0, 18); 
         lstDisplayActivities.Items.Add(line); 
        } 
        else 
        { 
         MessageBox.Show("No Files were found"); 
        } 
       } 
      } 
     } 
    } 
+3

まあ、あなたはメッセージボックスを受け取っているのですが、すべての行に 'Event Name:'があります。 – CNuts

+0

この行はどうなると思いますか? _line.Skip(1).Take(2); _ – Steve

+0

何行あるかによって、そのメッセージを4/5回取得する必要があります。おそらくそれを 'while()'の終わりまで保持しておいてください – vipersassassin

答えて

0

@CNutsはコメントで言ったことについては詳しく説明し

if (line.Contains("Event Name:")) //When this line is found, 
{ 
    //Do stuff 
} 
else 
{ 
    MessageBox.Show("No Files were found"); 
} 

ないので、ファイル内のすべての行が含まれています。ここでは、コードである「イベント名:」行が毎回含まれていませんこの特定の文字列では、「ファイルが見つかりませんでした」というメッセージが表示されます。

bool eventsFound = false 
while (RetrieveEvents.Peek() != -1) 
{ 
    if (line.Contains("Event Name:")) //When this line is found, 
    { 
     //Do stuff 
     eventsFound = true; 
    } 
    line = RetrieveEvents.ReadLine(); 
} 
if(!eventsFound) 
    MessageBox.Show("No Files were found"); 
1

あなたが唯一のStreamReaderから最初の行を読んでいる:

は、ここで提案です。他の行は決して読み取られません。あなたは(ループを必要とし、各ループで再びラインを読ん

string line = RetrieveEvents.ReadLine();           
    while (RetrieveEvents.Peek() != -1) 
    { 
     if (line.Contains("Event Name:")) //When this line is found, 
     { 
      EventString = line.Remove(0, 12);       
      lstDisplayActivities.Items.Add(line); 

     } 
     else if(line.Contains("Event Type:")) 
     { 
      EventType = line.Remove(0, 12); 
      lstDisplayActivities.Items.Add(line); 
     } 
     else if(line.Contains("People Attending:")) 
     { 
      EventPeopleAttending = line.Remove(0, 18); 
      lstDisplayActivities.Items.Add(line); 
     } 

     // This reads the next line..... 
     line = RetrieveEvents.ReadLine() 
    } 
} 

はライン

line.Skip(1).Take(2) 

と同類は、次の行に進みませんが、彼らは単に行の文字数をスキップ1)それから何かにそれを割り当てないすべてを捨てるために次の2つの文字を取る

関連する問題