尝试根据用户变量条目从文本文件中读取
我的条目有名称“Big Fred”的变体(大写/小写)
代码运行,但我没有得到任何结果。
我笔记本电脑上的代码指向我从下面的代码中删除的特定位置。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ReadTextFileWhile
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the name you wish to search for: "); //Prompt user for the name they wish to search for
string x = Console.ReadLine(); //assign the name the user inputs as their search parameter to the value of x
StreamReader myReader = new StreamReader("C:/insert location here"); //Read the the text file at this location and assign to myReader
string line = ""; //asssign 'nothing' to line.
while (line != null) //while line in the text file is not null (empty)
{
line = myReader.ReadLine(); //pass contents of myReader to line
if (line != null && line == x) //if contents of line are not null and equal to the variable in x print to screen
Console.WriteLine(line);
}
myReader.Close(); //close myReader properly
Console.ReadLine(); //Readline to keep console window open allowing a human to read output.
}
}
}