2 Replies - 31749 Views - Last Post: 01 February 2008 - 03:31 PM Rate Topic: -----

#1 dandes   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-February 08

Copying multiple files from open file dialog

Posted 01 February 2008 - 10:37 AM

I am trying to copy multiple files from an Open Files Dialog. The path of the files selected to copy should be displayed in a richtext doc in the form. At on point, the files selected displayed...but now they do not. I also managed to get one file to copy...but in revising the code...canot get back to that point where everything almost worked. Any suggestions would be appreciated.


	Dim strFileName As String = ""
	Dim path As String = "C:\"


  Private Sub btnSendFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendFiles.Click
		Dim TotalFile As String = ""
		OpenFD.Multiselect = True
		Dim CopyLoc = path & strFileName

		If OpenFD.ShowDialog = DialogResult.OK Then
			For Each s As String In OpenFD.FileName
				TotalFile = TotalFile & s & Environment.NewLine
				rtxFiles.Text = TotalFile
				File.Copy(OpenFD.FileName, CopyLoc & "\" & OpenFD.FileName.Substring(OpenFD.FileName.LastIndexOf("\")))
			Next

		End If
	End Sub


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 01 February 2008 - 11:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Copying multiple files from open file dialog

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Copying multiple files from open file dialog

Posted 01 February 2008 - 12:09 PM

If by copying you mean copy the file itself from one folder to C:\ then you can use something like this... (a modification of your function)

Dim strFileName As String = ""
Dim path As String = "C:\"


Private Sub btnSendFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendFiles.Click

		Dim TotalFile As String = ""
		OpenFD.Multiselect = True

		If OpenFD.ShowDialog = DialogResult.OK Then
			' For each filename in the list of selected FileName"S"
			For Each s As String In OpenFD.FileNames
				' Append the filename on a line and add the newline char
				rtxFiles.Text &= s & Environment.NewLine

				' Use the filename as the source, then build the path to C:\ using the filename of the current file.
				' Result, all files selected in the openfile dialog will be copied to C:\ with the same names.
				' eg. D:\miscfolder\helloworld.vb will be copied to C:\ as C:\helloworld.vb
				File.Copy(s, path & System.IO.Path.GetFileName(s))
			Next

		End If
End Sub



The in code comments will tell you what is going on at each step and hopefully this is what you were looking to do with this code. If not, can you be more specific as to your intentions? Thanks!

"At DIC we be file copying code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 dandes   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-February 08

Re: Copying multiple files from open file dialog

Posted 01 February 2008 - 03:31 PM

That works great! Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1