/// <summary>
/// 根据要查看的日期找到指定文件再根据文件内容按照指定格式进行读取
/// </summary>
/// <param name="datetime">参数应该是dateTimePicker1控件中选择的日期</param>
public string ReadFile(DateTime datetime)
{
//根据datetime找出要读的文件的路径
string fileName = datetime.ToShortDateString() + ".txt";
string filePath = "E:\\" + fileName;
if (!File.Exists(filePath))
{
MessageBox.Show("您要查找的文件不存在");
return null;
}
else
{
string result = string.Empty;
//文件实例化streamreader对象
StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("gb2312"));
//调用readertoend 方法读取选中文件的全部内容
result = sr.ReadToEnd();
//关闭当前文件读取流
sr.Close();
return result;
}
}
/// <summary>
/// 根据时间创建文件
/// </summary>
/// <param name="datetime">发送邮件的时间</param>
public void CreateFile(DateTime datetime)
{
string fileName = datetime.ToShortDateString() + ".txt";
string filePath = "E:\\" + fileName;
FileInfo fileInfo = new FileInfo(filePath);
if (!fileInfo.Exists)
{
fileInfo.Create();
}
else
{
//提示文件已经存在
MessageBox.Show("文件已存在");
}
}
/// <summary>
/// 把datatable中的数据按照时间写入到txt文件中
/// </summary>
/// <param name="receivename">收件人姓名</param>
/// <param name="address">收件人地址</param>
/// <param name="sendtime">邮件发送具体时间</param>
/// <param name="record">发送信息报告</param>
/// <param name="filename">定义要写入到的文件名</param>
public static void WriteToFile(string receivename, string address, DateTime sendtime, string record)
{
string filename = sendtime.ToShortDateString() + ".txt";
string filePath = "E:\\" + filename ;
FileStream fileStream = new FileStream(filePath , FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fileStream, Encoding.GetEncoding("gb2312"));
string content = receivename + address + sendtime + record;
sw.WriteLine(content);
sw.Close();
fileStream.Close();
}