2017-10-09 34 views
0

在那裏,我有布爾的文件添加到列表框:是否可以使用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 

是否可以顯示列表框中只有文件名,但它會包含在第二布爾使用它的路徑?

因爲當我使用sp.SafeFileNames時,它無法發送,因爲我沒有路徑。

+0

這非常聽起來像你有有2個不同的問題 - 一個關於FileOpenDialog和其他有關列表框的成員。請澄清 – 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

謝謝,完美的作品。 –

相關問題