【发布时间】:2018-08-21 07:33:08
【问题描述】:
您好我有以下代码,我面临的问题是无论文件夹是否存在它仍然继续发送电子邮件而不是忽略发送它。
我可以改变什么来让它工作。
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)
{
}
}
}
【问题讨论】:
-
100% 路径错误。调试您的代码并检查
path值。此外,您可以使用 DateTime.Day/.Month/.Year 属性来访问您需要的值,而无需解析string。 -
除了某人给出的答案之外,您可能无权访问其他用户的桌面文件夹,因此
Directory.Exists()正在返回false。如果修复日期格式MM后不起作用,请尝试以管理员身份运行程序。 -
另外,不必将日期转换为字符串,然后提取日/月/年组件。使用 DateTime.Day、.Month 和 .Year。
-
@Fildor 我在等着看其他答案是否能先解决问题...
标签: c# if-statement directory