C# 与StartsWith或Substring的字符串比较无效
我需要找到我从电子邮件中下载的附件,但我无法将附件文件名与字符串进行比较,我做错了什么?脚本应该返回“in-in”,但它返回“out” 输出:C# 与StartsWith或Substring的字符串比较无效,c#,.net,C#,.net,我需要找到我从电子邮件中下载的附件,但我无法将附件文件名与字符串进行比较,我做错了什么?脚本应该返回“in-in”,但它返回“out” 输出: OpenOrders some text with spaces.xlsx out out 您正在输出fileAttachment.Name,但在StartsWith中使用fileAttachment.FileName。使用正确的版本,它应该可以工作 FileAttachment fileAttachment = item.Attachments[0]
OpenOrders some text with spaces.xlsx
out
out
您正在输出
fileAttachment.Name
,但在StartsWith
中使用fileAttachment.FileName
。使用正确的版本,它应该可以工作
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");
}
您正在输出
fileAttachment.Name
,但在StartsWith
中使用fileAttachment.FileName
。使用正确的版本,它应该可以工作
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");
}
您正在输出
名称
属性:
Console.WriteLine(fileAttachment.Name);
但是您的
StartsWith
和Substring
调用是针对FileName
属性的。我怀疑您会发现Name
返回的内容与您输出的Name
属性不同:
Console.WriteLine(fileAttachment.Name);
但是您的
StartsWith
和Substring
调用是针对FileName
属性的。我怀疑您会发现Name
返回了与FileName
不同的内容fileAttachment.FileName
的值是多少?请注意,StartsWith(string)
方法会进行区分大小写和区域性的比较。fileAttachment.FileName
与fileAttachment.Name
可能是完整路径。啊,ohmygo谢谢,我太盲目了,难以置信!!fileAtachment.Name work!。fileAttachment.FileName的值是多少?请注意,StartsWith(string)
方法会进行区分大小写和区域性的比较。fileAttachment.FileName
与fileAttachment.Name
可能是完整路径。啊,ohmygo谢谢,我太盲目了,难以置信!!fileAtachment.Name work!。谢谢,它很有效。我是如此的盲目以至于我甚至没有意识到我使用的是不同的方法:谢谢你,它是有效的。我是如此的盲目以至于我甚至没有意识到我在使用不同的方法:p