Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# StreamReader在文件中找不到字符串_C#_Winforms_Visual Studio 2015_Streamreader - Fatal编程技术网

C# StreamReader在文件中找不到字符串

C# StreamReader在文件中找不到字符串,c#,winforms,visual-studio-2015,streamreader,C#,Winforms,Visual Studio 2015,Streamreader,我对蒸汽阅读器有意见。我的StreamReader在文件中找不到特定的字符串匹配项,即使我知道该字符串存在。最好的方式是通过图像来显示这一点。你会更好地理解它是如何工作的。我将在以下情况下提供代码: 此图显示检查字符串是否在文件中的代码。打开的文本文档是选中的文件。在输入信息后创建新表单(此表单)时,它作为变量传递。表单按钮是运行代码的按钮,如图所示。如果未找到字符串,则显示消息框 正如您所看到的,该文件包含字符串,但没有拾取它:/ 我想这很简单,但我需要一双新的眼睛。代码如下: privat

我对蒸汽阅读器有意见。我的StreamReader在文件中找不到特定的字符串匹配项,即使我知道该字符串存在。最好的方式是通过图像来显示这一点。你会更好地理解它是如何工作的。我将在以下情况下提供代码:

此图显示检查字符串是否在文件中的代码。打开的文本文档是选中的文件。在输入信息后创建新表单(此表单)时,它作为变量传递。表单按钮是运行代码的按钮,如图所示。如果未找到字符串,则显示消息框

正如您所看到的,该文件包含字符串,但没有拾取它:/

我想这很简单,但我需要一双新的眼睛。代码如下:

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");
                    }
                }
            }
        }
    }

详细说明@CNuts在评论中所说的内容

if (line.Contains("Event Name:")) //When this line is found,
{
     //Do stuff
}
else
{
    MessageBox.Show("No Files were found");
}
由于并非文件中的所有行都包含“Event Name:”因此,每当该行不包含此特定字符串时,您将收到消息“未找到任何文件”

这里有一个建议:

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");

您只读取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个字符,只是为了丢弃所有你没有分配给任何东西的内容。

很正常,你会收到消息框,不是所有的行都有
事件名称:
。你认为这一行可以做什么?行。跳过(1)。采取(2);根据有多少行代码,您应该会收到该消息4/5次。可能要等到
while()
@viperschiller的结尾,他只读了一行。它将永远看不到其他行。StreamReader是浪费时间,除非您希望文件很大。只需
File.ReadAllLines
并处理生成的行数组。调试时也更容易检查。