Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 与StartsWith或Substring的字符串比较无效_C#_.net - Fatal编程技术网

C# 与StartsWith或Substring的字符串比较无效

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]

我需要找到我从电子邮件中下载的附件,但我无法将附件文件名与字符串进行比较,我做错了什么?脚本应该返回“in-in”,但它返回“out”

输出:

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