嗨,我有以下代码,无论文件夹是否存在,我都面临的问题是它仍然继续发送电子邮件,而不是忽略发送电子邮件。
要使该功能正常运行,我需要进行哪些更改。
static void Main(string[] args)
{
string yesterdaydate = DateTime.Now.AddDays(-1).ToString("yyyy-mm-dd");
string[] SplitDate = yesterdaydate.Split('-');
string year = SplitDate[0];
string month = SplitDate[1];
string day = SplitDate[2];
string path = Path.Combine("C:\\Users\\ales\\Desktop\\test", year, month, day);
if (Directory.Exists(path))
{
//do nothing
}
else
{
string fromAddress = "noreply@arm.com";
string toAddress = "alese@arm.com";
string subject = "error";
string body = "failed to sync";
krysalis_email.EmailClient email = new krysalis_email.EmailClient();
krysalis_email.EmailClient.EmailResponse emailResponse = email.sendBasicMail(new object[] {toAddress}, fromAddress, subject, body, false, "smtp.za.arm.com",
new string[] {"", ""}, false, null);
if (emailResponse != null)
{
}
}
}
答案 0 :(得分:2)
问题在于您将日期格式转换为字符串。您正在使用mm
,这是分钟。使用MM
获取月份。请记住,MM
格式将为您提供前导零的月份,例如08
如果要使用字符串拆分,请将代码更改为
string yesterdaydate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
但是,正如其他人指出的那样,获取值的更好方法是使用DateTime
而不是解析字符串。这是一个示例:
DateTime yesterdaydate = DateTime.Now.AddDays(-1);
string year = yesterdaydate.Year.ToString();
string month = yesterdaydate.Month.ToString("D2");//D2 to format number to be zero-padded
string day = yesterdaydate.Day.ToString("D2");
string path = Path.Combine("C:\\Users\\ales\\Desktop\\test", year, month, day);