C# 通过电子邮件发送csv行
我想在csv文件中查找日期,例如,我想以电子邮件的形式发送此行 亚当·斯密特;2014年12月1日im使用此代码,但身体总是空的我想知道是什么原因造成的? 谢谢你的帮助C# 通过电子邮件发送csv行,c#,csv,filereader,C#,Csv,Filereader,我想在csv文件中查找日期,例如,我想以电子邮件的形式发送此行 亚当·斯密特;2014年12月1日im使用此代码,但身体总是空的我想知道是什么原因造成的? 谢谢你的帮助 System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.csv"); while ((line = file.ReadLine()) != null) { int i = 0; string[] words = line.Sp
System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.csv");
while ((line = file.ReadLine()) != null)
{
int i = 0;
string[] words = line.Split(";"[0]);
if (words[i] == DateTime.Now.ToString("dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture))
{
mail.Body = line;
}
i++;
counter++;
}
words[i]始终为您提供0项,因为您不在其中执行任何循环。因此,不是:
if (words[i] == DateTime.Now.ToString("dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture))
{
使用
您不需要i变量和i++增量变量声明[i]应该在控制循环之前,而不是在控制循环中。您的变量
i
始终为0。您应该将您的行附加到mail.body
中,而不是覆盖整个正文
试试这个:
var file = new System.IO.StreamReader(@"c:\test.csv");
while ((line = file.ReadLine()) != null)
{
string[] words = line.Split(';');
DateTime dt;
if(DateTime.TryParse(words[1], new CultureInfo("de-DE"), DateTimeStyles.None, out dt)
&& dt.Date == DateTime.Today)
{
mail.Body += line + Environment.NewLine;
}
}
日期在
字[1]
中。另外,您的日期时间格式应该是“d.MM.yyyy”
(或者“d.M.yyyy”
)words[i]为您提供了一整行。错误的
var file = new System.IO.StreamReader(@"c:\test.csv");
while ((line = file.ReadLine()) != null)
{
string[] words = line.Split(';');
DateTime dt;
if(DateTime.TryParse(words[1], new CultureInfo("de-DE"), DateTimeStyles.None, out dt)
&& dt.Date == DateTime.Today)
{
mail.Body += line + Environment.NewLine;
}
}