问题描述
我正在做Windows项目,其中有两个DateTimePicker控件,一个用于StartDate,另一个用于EndDate。在运行时,当用户选择StartDate和EndDate从该控件,它应该读取文本文件(即)log.txt&搜索一行一行获得这两个日期之间以及日期之间的匹配,并将数据写入文本框或标签控件
,例如在日志文件中,数据如下:
3/12/2013 2:51:47 PM - ASDASDASD.D20131203145019
4/12/2013 2:52:23 PM - ASDDFSDSA.C20131203145019
5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D
6/12/2013 3:17:11 PM - RRRTWEWA_20131203184602.D00
7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408
如果我从2013年5月5日到2013年7月7日搜索,应该检索:
5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D
6/12/2013 3:17:11 PM - RRRTWEWA_20131203184602.D00
7/12/2013 3 :35:32 PM - XBCNXCXCXC.D0120131203153408
但到现在为止:
5/12 / 2013 2:52:37 PM - SDASAFAS_20131203182101.D
7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408
我正在追回StartDate& EndDate匹配数据不在日期数据之间。
以下是我尝试过的一些编码:
try
{
// ...
string FDDate = FD.Date.ToString(M / d / yyyy);
string TDDate = TD.Date.ToString(M / d / yyyy);
string searchstring = EnterFileNameTextbox.Text.ToString();
string searchfromdate = FromDateTimePicker.Value.ToShortDateString();
string searchtodate = ToDateTimePicker.Value.ToShortDateString();
string line;
StringBuilder sb = new StringBuilder();
int counter = 0;
使用(StreamReader file = new StreamReader(@D:\log.txt))
{
while((line = file.ReadLine())!= null)
{
if(line.Contains(searchstring)&& line.Contains(FDDate))
{
sb.AppendLine(line.ToString());
counter ++;
}
else if(line.Contains(searchstring)&!line.Contains(FDDate))
{
if(FD.Date< TD.Date)
{
sb.AppendLine(line.ToString());
counter ++;
FDDate = FD.Date.AddDays(1).ToShortDateString();
}
else if(FD.Date == TD.Date)
{
FDDate = FD.Date.ToShortDateString();
sb.AppendLine(line.ToString());
counter ++;
}
}
}
}
ResultTextBox.Text = sb.ToString();
CountLabel.Text = counter.ToString();
}
catch(Exception ex)
{
MessageBox.Show(该文件无法读取);
MessageBox.Show(ex.Message);
}
我没有想到要循环。
我建议使用 DateTime.TryParse
(doc )而不是转换。这将允许您提供有效的格式化,处理失败(如果失败,转换将抛出)。
关于使用属性似乎很好,比较运算符将按预期工作。
I am doing Windows project, in which I have two DateTimePicker controls, one for StartDate and other for EndDate.
In runtime, when user selects the StartDate and EndDate from that controls, it should read the textfile(i.e) log.txt & search line by line get the matching of both these dates and as well as in between dates and write the data to a textbox or label control
for eg in log file, data is like below:
3/12/2013 2:51:47 PM - ASDASDASD.D20131203145019 4/12/2013 2:52:23 PM - ASDDFSDSA.C20131203145019 5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D 6/12/2013 3:17:11 PM - RRRTWEWA_20131203184602.D00 7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408
if I search from 5 dec 2013 to 7 dec 2013, it should retrieve:
5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D 6/12/2013 3:17:11 PM - RRRTWEWA_20131203184602.D00 7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408
but up to now I am getting:
5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D 7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408
I am retriving only the StartDate & EndDate matching data not in between date data.
The below are some of the coding I have tried:
try
{
// ...
string FDDate = FD.Date.ToString("M/d/yyyy");
string TDDate = TD.Date.ToString("M/d/yyyy");
string searchstring = EnterFileNameTextbox.Text.ToString();
string searchfromdate = FromDateTimePicker.Value.ToShortDateString();
string searchtodate = ToDateTimePicker.Value.ToShortDateString();
string line;
StringBuilder sb = new StringBuilder();
int counter = 0;
using (StreamReader file = new StreamReader(@"D:\log.txt"))
{
while ((line = file.ReadLine()) != null)
{
if (line.Contains(searchstring) && line.Contains(FDDate))
{
sb.AppendLine(line.ToString());
counter++;
}
else if (line.Contains(searchstring) && !line.Contains(FDDate))
{
if (FD.Date < TD.Date)
{
sb.AppendLine(line.ToString());
counter++;
FDDate = FD.Date.AddDays(1).ToShortDateString();
}
else if (FD.Date == TD.Date)
{
FDDate = FD.Date.ToShortDateString();
sb.AppendLine(line.ToString());
counter++;
}
}
}
}
ResultTextBox.Text = sb.ToString();
CountLabel.Text = counter.ToString();
}
catch (Exception ex)
{
MessageBox.Show("The file could not be read");
MessageBox.Show(ex.Message);
}
I am not getting idea to do the loop.
I would recommend using DateTime.TryParse
(doc here) instead of just converting. That will allow you to provide valid formatting, handle failures (convert will throw if it fails).
The part about using the Date property seems fine, and comparison operators will work as expected.
这篇关于如何比较datetimepicker中的两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!