AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
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?

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > ran into problem with one of your answers

Topic: VB.NET



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

Hope this helps!

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.