About Chris Expertise I can answer pretty much any question relating to VB.NET and its use in a Windows environment. I specialize in ASP.NET web development and MSSQL database access.
Experience I have over 5 years of industry experience using VB.NET and other .NET technologies for web and database development.
Education/Credentials I have some college education, but does it really matter in this field of work?
Expert: Chris Date: 9/1/2007 Subject: ran into problem with one of your answers
Question Hello,
I found your answers on this page (http://en.allexperts.com/q/VB-NET-3306/List-box-open-file-1.htm?zIr=5) very helpful. I copied and pasted everything exactly and it works, however I am still getting the full pathname of the file(s) I have added to a listbox after using FileInfo, rather than just the filename and extension itself. Have I missed something?
Here is the code I use to add to a listbox:
For Each strFileName As String In AttachmentDialog.FileNames
lstAttachments.Items.Add(New FileInfo(strFileName))
Next
Best regards,
KC
Answer Hi,
Thanks for pointing this out to me. Seems like it works sometimes, and not others. So, instead of adding a new FileInfo(...) to the listbox, add one of these MyFileInfo objects instead (and use it's .FileInfo property to get the internal FileInfo object, if you need it):
'--[ MyFileInfo.vb ]--
Imports System.IO
Public Class MyFileInfo
Protected m_objFI As FileInfo = Nothing
Public ReadOnly Property FileInfo() As FileInfo
Get
Return m_objFI
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return m_objFI.Name
End Get
End Property
Public ReadOnly Property FullName() As String
Get
Return m_objFI.FullName
End Get
End Property
Private Sub New()
End Sub
Public Sub New(ByVal objFI As FileInfo)
m_objFI = objFI
End Sub
Public Sub New(ByVal strFilename As String)
m_objFI = New FileInfo(strFilename)
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
'--[ end MyFileInfo.vb ]--
For example:
For Each strFileName As String In AttachmentDialog.FileNames
lstAttachments.Items.Add(New MyFileInfo(strFileName))
Next
Dim objFileInfo as FileInfo = DirectCast(lstAttachments.SelectedItem, MyFileInfo).FileInfo