Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Open multiple files
 

Open multiple files

Just as the subject says, need help whit open multiple files... Right now i´m doing like this when I´m opening a file in my app.

Code Block

Dim myStream As Stream

Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.InitialDirectory = "c:\"

openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"

openFileDialog1.FilterIndex = 2

openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

myStream = openFileDialog1.OpenFile()

If Not (myStream Is Nothing) Then

myStream.Close()

End If

End If

But now I wont to be able to mark several files in the openfiledialog and then in my form be able to see witch files that was marked? how should i do this??

I think i should add OpenFileDialog1.Multiselect = True but then i dont know what more to do?!

Please help me

A_I_T  Thursday, December 13, 2007 4:48 PM

THe OpenFileDialog is just that a dialog which doesnt do more than present a consistent user interface for selevting filenames. Using MultiSelect property to try allows selecting multiple filenames by holding down the Ctrl or shift keys when selecting filenames which is consistent with other windows applications.

TO see which filenames where selected then you can iterate round the Filenames Collection. In this example I display a dialog - the user can select one or many filenames and when they click ok, it will build a single string with each of the selected filenames and display a messagebox.

Obviously the iterating loop if where you would process each of the the selected files.

Code Block

Dim x As New OpenFileDialog

x.Multiselect = True

If x.ShowDialog = Windows.Forms.DialogResult.OK Then

'Build a string with all the filenames selected

Dim TotalFIle As String = ""

For Each s As String In x.FileNames

TotalFIle = TotalFIle & s & Environment.NewLine

Next

MessageBox.Show(TotalFIle)

End If

spotty  Thursday, December 13, 2007 6:07 PM

THe OpenFileDialog is just that a dialog which doesnt do more than present a consistent user interface for selevting filenames. Using MultiSelect property to try allows selecting multiple filenames by holding down the Ctrl or shift keys when selecting filenames which is consistent with other windows applications.

TO see which filenames where selected then you can iterate round the Filenames Collection. In this example I display a dialog - the user can select one or many filenames and when they click ok, it will build a single string with each of the selected filenames and display a messagebox.

Obviously the iterating loop if where you would process each of the the selected files.

Code Block

Dim x As New OpenFileDialog

x.Multiselect = True

If x.ShowDialog = Windows.Forms.DialogResult.OK Then

'Build a string with all the filenames selected

Dim TotalFIle As String = ""

For Each s As String In x.FileNames

TotalFIle = TotalFIle & s & Environment.NewLine

Next

MessageBox.Show(TotalFIle)

End If

spotty  Thursday, December 13, 2007 6:07 PM

Thank you spotty it works... But how do i for examply use the lines. lets say that i wont to copy the selected files with a Do-loop. And i wont to start at the fist line (fist file) and then continue upwards and if i select maby five files a wont the do loop to stop afterwards 5?

how do I read the diffrent lines?

and how do i findout how many files that have been selected?

A_I_T  Thursday, December 13, 2007 6:22 PM

Filenames is simply a collection of the selected files names in the dialog.

How you decided to process them is down to you. In this case I simply iterated throuhg the files using the selected filename collection using the for each construct but you can use other looping constructs if you wanted to such as

For i = 0 To x.FileNames.Count

Next

Its a collection of strings - how you want to process this collection is really down to you.

How to find out how many files have been selected - use the Filenames.Count property.

Might I suggest you look at the documentation relating to this control

http://msdn2.microsoft.com/en-us/library/system.windows.forms.filedialog.filenames.aspx

and read a little on VB.NET + collections as this is fundamental in many of other properties and code used in VB.NET.

spotty  Thursday, December 13, 2007 7:19 PM

Well I manedge to count the numbers of files that have been selected but cant readout the diffrent paths... Anyone got a sample how you usaly do to share with me?

A_I_T  Sunday, December 16, 2007 11:36 AM

You can use google to search for other answers

Custom Search

More Threads

• Mouse movement within a form in VB.Net
• Replace in SQL on Access Database
• Launch URL from Windows Service
• Newbie, ASAP EMERGENCY HELP-USB Flash Drive Serial Number in VB.net
• Some Array Help Please
• VB Variables (Reference)
• Data Queues
• LINQ when queries execute, a little understanding please
• Need Help with a System.Windows.Forms.BindingNavigator
• Application Sharing