Get files names into array

ddady

Member
Joined
Sep 19, 2006
Messages
20
Programming Experience
Beginner
Hi all!

What i'm trying to do is to open a dialog box which from there i will be able to select files and their names will go into an array.

Any help will be appreciated :)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
The FileNames property returns a String array containing the selected filenames, so you already have that array.
 

ddady

Member
Joined
Sep 19, 2006
Messages
20
Programming Experience
Beginner
Ok i have this code: the "lboxResult" is a listbox.

VB.NET:
Private Sub pxBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pxBrowse.Click
        Dim dr As DialogResult = Me.OpenFileDialog1.ShowDialog()
        If (dr = System.Windows.Forms.DialogResult.OK) Then
            
            Dim file As String
            For Each file In OpenFileDialog1.FileNames
              
                Try
                    lboxResult.Items.Add(IO.Path.GetFileName(OpenFileDialog1.FileName))
                    
                Catch SecEx As Security.SecurityException
                    
                    MessageBox.Show("Security error. Please contact your administrator for details.\n\n" & _
                        "Error message: " & SecEx.Message & "\n\n" & _
                        "Details (send to Support):\n\n" & SecEx.StackTrace)
                Catch ex As Exception
                    
                    MessageBox.Show(("Cannot display the image: " & file.Substring(file.LastIndexOf("\"c)) & _
                    ". You may not have permission to read the file, or " + "it may be corrupt." _
                    & ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))
                End Try
            Next file
        End If
        
    End Sub

It works fine when i choose one file at the time, but when i choose serveral files in one time it shows in the listbox only the first one as many times as the number of the files i have chosen.

What do i do wrong? or what am i missing?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
This is also a way to read your code:
For Each file in Filenames
ignore file and use something else instead
Give that a think :)
 

phicha

Well-known member
Joined
May 11, 2009
Messages
45
Programming Experience
Beginner
Imports System.IO

then for retrive files as array from directory, use like this sintax.

Dim ad As Directory
Dim file2() As String = ad.GetFiles("c:\")
For Each str As String In file2
MsgBox(str)
Next
 

BlackByte

Well-known member
Joined
Jun 29, 2008
Messages
126
Location
South Africa, Durban
Programming Experience
1-3
You are putting OpenFileDialog.FileName instead of "File"
here
lboxResult.Items.Add(IO.Path.GetFileName(OpenFileDialog1.FileName)) 'Put file instead of OpenFileDialog1.FileName
This should work
VB.NET:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each s As String In ofd.FileNames
                ListBox1.Items.Add(IO.Path.GetFileName(s))
            Next
        End If
    End Sub
 
Last edited:
Top Bottom