2017-10-09 3 views
0

あり、私はリストボックスにファイルを追加BOOL持っている:電子メールにファイルを追加OpenDialog.SafeFileNameで添付ファイルを送信することはできますか?

If sp.ShowDialog() = DialogResult.OK Then 
    For Each wp In sp.FileNames 
     ListBox1.Items.Add(wp) 
    Next wp 
End If 

とブール値を:

If ListBox1.Items.Count <> 0 Then 
    For Each file In ListBox1.Items 
     attch = New System.Net.Mail.Attachment(file) 
     message.Attachments.Add(attch) 
    Next file 
End If 

は、それがリストボックスにファイル名だけを表示することは可能ですが、それは含まれています2番目のブールで使用するパス?

sp.SafeFileNamesを使用すると、パスがないため送信できなかったためです。

+0

あなたがそこに2つの異なる質問持っているようにそれは非常に聞こえる - FileOpenDialog約1のListBoxメンバーについて別のものを。 – Plutonix

+0

@Plutonix私は自分のListBoxに、添付ファイルとして送信できるファイル名のみを入れたいと思います。しかし、SafeFileNamesバリアントを使用している場合、System.Net.Mail.Attachment()関数で使用されるパスは含まれていません。 –

+0

[.NETの実際のファイルをリストボックスから削除する]の可能な複製(https://stackoverflow.com/questions/46503196/net-delete-actual-files-from-listbox) –

答えて

0

あなたの項目を提示するDisplayMemberを使用することができます。

Imports System.IO 

Public Class Form1 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     ' add some selected files and show only their name in list 

     Dim dialog As New OpenFileDialog 

     If dialog.ShowDialog() <> DialogResult.OK Then Return 

     Dim attachments = dialog.FileNames.Select(function(s) new Attachment(s)).ToArray() 

     ListBox1.Items.AddRange(attachments) 
     ListBox1.DisplayMember = "FileName" 
    End Sub 

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 

     ' do something with the attachments 

     Dim attachments = ListBox1.Items.Cast (Of Attachment) 

     For Each attachment In attachments 
      Dim fullPath = attachment.FullPath 
      Console.WriteLine(fullPath) 
     Next 
    End Sub 
End Class 

Public Class Attachment 
    Public ReadOnly Property FileName As String 
     Get 
      Return Path.GetFileName(FullPath) 
     End Get 
    End Property 

    Property FullPath As String 

    Public Sub New(fullPath As String) 
     Me.FullPath = fullPath 
    End Sub 
End Class 
+0

ありがとう、完璧に動作します。 –

関連する問題