我需要找到我从电子邮件中下载的附件,但我无法将附件文件名与字符串进行比较,我做错了什么?脚本应该返回“in in”但它返回“out out”
FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;
Console.WriteLine(fileAttachment.Name);
if (fileAttachment.FileName.StartsWith("OpenOrders")) {
Console.WriteLine("in");
}
else {
Console.WriteLine("out");
}
if (fileAttachment.FileName.Substring(0, 10) == "OpenOrders") {
Console.WriteLine("in");
} else {
Console.WriteLine("out");
}
输出:
OpenOrders some text with spaces.xlsx
out
out
答案 0 :(得分:2)
您正在输出fileAttachment.Name
,但在fileAttachment.FileName
中使用StartsWith
。使用正确的版本,它应该工作。
FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;
Console.WriteLine(fileAttachment.Name);
if (fileAttachment.Name.StartsWith("OpenOrders")) {
Console.WriteLine("in");
}
else {
Console.WriteLine("out");
}
if (fileAttachment.Name.Substring(0, 10) == "OpenOrders") {
Console.WriteLine("in");
} else {
Console.WriteLine("out");
}
答案 1 :(得分:2)
您正在输出Name
属性:
Console.WriteLine(fileAttachment.Name);
但您的StartsWith
和Substring
来电是针对FileName
财产的。我怀疑您会发现Name
正在返回与FileName
不同的内容。