9 Replies - 4242 Views - Last Post: 17 April 2007 - 09:51 PM Rate Topic: -----

#1 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Copy File chosen from openfiledialog

Posted 16 April 2007 - 07:27 PM

So I've managed to get my "openfiledialog" to work AND output the user selected file(s) as "OpenFileDialog1.FileName".

The problem is I now want the action to copy said file to a temp folder I've created.
Base on 3 hours of searching, heres what I wrote:

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
		OpenFileDialog1.ShowDialog()
		Dim Source_dol_gcm As String
		Source_dol_gcm = OpenFileDialog1.FileName
		Dim Dest_dol_gcm As String
		Dest_dol_gcm = "\Dols" & Source_dol_gcm
		FileSystem.FileCopy(Source_dol_gcm, Dest_dol_gcm)
End Sub


Everytime I run in debug, I get an error telling me The given path's format is not supported.

The folder \Dols is under the "bin\debug\" folder so I know it should be able to place it there.

Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Copy File chosen from openfiledialog

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Copy File chosen from openfiledialog

Posted 16 April 2007 - 09:00 PM

You need to provide the absolute path to where you want to store your copy.

Since you have it in the Debug folder. You can use Application.StartUpPath to give you the absolute path to that particular folder, then all you need to do is concatenate the rest of the path into the string.
Dest_dol_gcm = Application.StartUpPath & "\Dols" & Source_dol_gcm


Was This Post Helpful? 1

#3 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Copy File chosen from openfiledialog

Posted 16 April 2007 - 10:02 PM

View Postjayman9, on 16 Apr, 2007 - 09:00 PM, said:

You need to provide the absolute path to where you want to store your copy.

Since you have it in the Debug folder. You can use Application.StartUpPath to give you the absolute path to that particular folder, then all you need to do is concatenate the rest of the path into the string.
Dest_dol_gcm = Application.StartUpPath & "\Dols" & Source_dol_gcm




Doents work. :crazy:

I think that FileCopy() refuses to take any strings other than absolute paths.
Thats why it wont accept my defenitions.

I even tryed using a process.start() line using CMD and "copy", worked- but i couldn't get the path correct for some reason. no matter what, it was placing the file in C:\DOLs\ instead of in the app path.

I'll try using the "Application.StartUpPath" option in the "process.start" next.

Thanks for the suggestion... theres gotta be a lightweight and solid way of doing this.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Copy File chosen from openfiledialog

Posted 17 April 2007 - 10:23 AM

The problem is that you are stacking two abosolute paths on top of each other, which of course is invalid.

You need to get just the filename from your first String Source_dol_gcm, currently this is storing the absolute path to the file you want to copy, you will need to extract just the filename from Source_dol_gcm and add it onto the end of Dest_dol_gcm.

I would suggest using LastIndexOf method of String to find the last "\" and then use the Substring method to get the filename only from the that index number to the end of the string.
Was This Post Helpful? 0
  • +
  • -

#5 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Copy File chosen from openfiledialog

Posted 17 April 2007 - 02:29 PM

Ok.. implemented this:

If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
			Try
				Dim sourcedolgcm As String
				sourcedolgcm = OpenFileDialog1.FileName
				Dim selected_index As String
				selected_index = OpenFileDialog1.FileName.LastIndexOf("\")
				Dim selected_file As String
				selected_file = selected_index.Substring(2)
				Dim destdolgcm As String
				destdolgcm = ".\Dols\" & selected_file
				FileCopy(sourcedolgcm, destdolgcm)
			Catch ex As Exception
				MsgBox("File Not Copied")
			End Try
		End If


But I get a "System.ArgumentOutOfRangeException"

I used the "string.lastindexof("\")
Retreived the index and extracted the substring based on the index #
(I learned the index # by bypassing the substring, and it created a "2" file in the destination directory)


Any suggestions?
Really appreciate your help BTW.
Was This Post Helpful? 0
  • +
  • -

#6 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Copy File chosen from openfiledialog

Posted 17 April 2007 - 02:46 PM

Never mind

Used:
Dim selected_file As String = OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\") + 1)


instead of all the other declerations and it works

thanks for your help
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Copy File chosen from openfiledialog

Posted 17 April 2007 - 03:30 PM

Excellent work. I knew you could do it. :)
Was This Post Helpful? 0
  • +
  • -

#8 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Copy File chosen from openfiledialog

Posted 17 April 2007 - 07:02 PM

Hmm... I hit one more snag. Should be pretty easy to adapt.
I'm going to need to be able to select multiple files for this process.
How would I tell it to do that?
The dialog allows me to select multiple, but the action of copying the selected files only copies the last highlighted file.
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Copy File chosen from openfiledialog

Posted 17 April 2007 - 09:32 PM

You can use OpenFileDialog.FileNames to retrieve multiple file names. The values returned will be an array of type String, so a simple String variable will not work.

MSDN FileDialog.FileNames Property.
Was This Post Helpful? 0
  • +
  • -

#10 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Copy File chosen from openfiledialog

Posted 17 April 2007 - 09:51 PM

View Postjayman9, on 17 Apr, 2007 - 09:32 PM, said:

You can use OpenFileDialog.FileNames to retrieve multiple file names. The values returned will be an array of type String, so a simple String variable will not work.

MSDN FileDialog.FileNames Property.



You Rock!
I feel like I should be paying you for this LOL
You really have helped more than you know. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1