Check if specific file exists in MapiMessage attachent collection?

Hello,
Is there any property/method to get a boolean checking if any of MapiMessage attachment names matches a specific string.
ie, if there’s an attachment named “blah.txt” in the attachments of MapiMessage?
Thanks for your help :slight_smile:

Hello @australian.dev.nerds,

You can try the following code example to check the existence of an attachment in the collection:

Public Function CheckAttachmentName(ByVal attachmentCollection As MapiAttachmentCollection, ByVal attachmentName As String) As Boolean
    Return attachmentCollection.Cast(Of MapiAttachment)().Any(Function(attachment) attachment.LongFileName.Equals(attachmentName, StringComparison.OrdinalIgnoreCase))
End Function

Dim attachmentNameToFind As String = "blah.txt"
Dim hasAttachment As Boolean = CheckAttachmentName(message.Attachments, attachmentNameToFind)
1 Like

Hello and thanks for your code sample,

I recall it was a bit more complex, I found this old code:

Dim MyFileName As String = String.Empty
If String.IsNullOrWhiteSpace(Attach.LongFileName) = False Then
    MyFileName = Attach.LongFileName
ElseIf String.IsNullOrWhiteSpace(Attach.FileName) = False Then
    MyFileName = Attach.FileName
ElseIf String.IsNullOrWhiteSpace(Attach.DisplayName) = False Then
    MyFileName = Attach.DisplayName
End If

If String.Equals(MyFileName, AttachmentName, StringComparison.OrdinalIgnoreCase) = True Then Return True

But I think there were cases that all 3 properties:
.LongFileName / .FileName / .DisplayName
were blank, what was your method to retrieve the file name of such attachments? Extract property or else?

Hello @australian.dev.nerds,

I don’t quite remember that case, but if you can find a message file with similar attachments, I can investigate it.