我正在做一个约会计划,其中我根据日期添加几个约会并存储在文本文件中。我根据输入的日期显示约会详细信息时出现问题。文本文件中存储的约会如下所示。
日期&时间:08/08/2013 09:30 AM人名:Shiv
日期&时间:08/08/2013 10:30 AM人名:Sanjay
日期&时间:10/08/2013 09:30 PM人名:Kumar
问题是当我输入搜索约会的任何日期时,假设该特定日期有2个约会,我的输出只显示一个约会。
示例:如果我输入日期08/08 / 2013,则输入日期的文本文件中存储了2个约会,但我的输出仅显示一个这样的约会
约会详情
08/08/2013 09:30 AM人名:Shiv
我的代码:
Console.WriteLine("Enter the date to search appointment in (dd/mm/yyyy) format");
string Date = Console.ReadLine();
string str = sr.ReadToEnd();
bool isDate = File.ReadAllText("E:\\Practice/C#/MySchedular.txt").Contains(Date) ? true : false;
if (isDate)
{
string searchWithinThis = str; ;
int CharacterPos = searchWithinThis.IndexOf(Date);
sr.BaseStream.Seek(CharacterPos ,SeekOrigin.Begin);
str = sr.ReadLine();
Console.WriteLine("\n*********Appointment Details*********");
Console.WriteLine("{0}", str);
Console.WriteLine("\n");
}
else
{
Console.WriteLine("No appointment details found for the entered date");
}
答案 0 :(得分:0)
也许这样的事情会起作用:
static void Main(string[] args)
{
string filename = @"E:\Practice\C#\MySchedular.txt";
string fileContent;
Console.WriteLine("Enter the date to search appointment in (dd/mm/yyyy) format");
string date = Console.ReadLine();
using (StreamReader sr = new StreamReader(filename))
{
fileContent = sr.ReadToEnd();
}
if (fileContent.Contains(date))
{
string[] apts = fileContent.Split('\n').Where(x => x.Contains(date)).ToArray();
foreach (string apt in apts)
{
Console.WriteLine("\n**Appointment Details**");
Console.WriteLine("{0}", apt);
Console.WriteLine("\n");
}
}
else
{
Console.WriteLine("No appointment details found for the entered date");
}
Console.Read();
}