12 Replies - 1381 Views - Last Post: 05 October 2011 - 07:32 PM Rate Topic: -----

#1 User_5573  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 12-November 10

Attempting To Move OpenFileDialog Result To Directory

Posted 04 October 2011 - 02:10 PM

I know that to move a file in VB.NET you use:
My.Computer.FileSystem.MoveFile("C:\Folder\File.ext", "C:\NewFolder\File.ext")


Where
"C:\Folder\File.ext"
is the file to move and
"C:\NewFolder\File.ext"
is the file's new location.


However, since the file being moved is the result of an OpenFileDialog I have no idea what file will be moved, but I know the directory it will be moved to.


My Code:
My.Computer.FileSystem.MoveFile(OpenFileDialog1.FileName, "C:\NewFolder\")


This will not work because I do not list the name of the file after
"C:\NewFolder\")


So, my question is: How can I move a file that is the result of an OpenFileDialog to a directory without knowing the file name and extension?

Is This A Good Question/Topic? 0
  • +

Replies To: Attempting To Move OpenFileDialog Result To Directory

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 04 October 2011 - 02:20 PM

If you need you can get the file name and its extension from the open file dialog or you can give a new name. To get the file name say:
OpenFileDialog1.SafeFileName()

Was This Post Helpful? 1
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8374
  • View blog
  • Posts: 31,123
  • Joined: 12-June 08

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 04 October 2011 - 02:21 PM

The IO.Path namespace has what you are looking for... conveniently called "GetFileName"!

http://msdn.microsof...em.io.path.aspx
Was This Post Helpful? 1
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,478
  • Joined: 16-March 11

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 04 October 2011 - 02:31 PM

You could always use the File, Fileinfo, Directory, and DirectoryInfo classes of System.io. :) The openfiledialog will provide the filename property, or filenames depending on whether or not you use multiselect. These filenames will be strings returned with the full path of the file you selected. Then you could use this info in conjunction with any of those classes. :)

File Class

Directory Class

I suggest you use these. :)

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        System.Io.File.Move(filepath, "C:\Users\Trevor\Desktop\VB.NET Programs\myFile" & filepath.Substring(filepath.LastIndexOf("."), 4))

    End Sub



Also you can use string methods to break down your filepath, and find that extension, and filename.

For example:

        Dim filepath As String = "C:\trevsfile\koll.exe"
        Dim filepart1 As String = filepath.Substring(filepath.LastIndexOf("\") + 1)
        Dim file() As String = filepart1.Split(".")
        MessageBox.Show(file(0) & ", " & file(1))


This post has been edited by trevster344: 04 October 2011 - 04:15 PM

Was This Post Helpful? 1
  • +
  • -

#5 User_5573  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 12-November 10

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 04 October 2011 - 02:57 PM

View Postmodi123_1, on 04 October 2011 - 02:21 PM, said:

The IO.Path namespace has what you are looking for... conveniently called "GetFileName"!

http://msdn.microsof...em.io.path.aspx


So how would I splice the result to the output directory?
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 04 October 2011 - 03:03 PM

Don't concatenate them as if they where just strings, they filepaths. So use Path.Combine.
Was This Post Helpful? 1
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 04 October 2011 - 03:04 PM

Does OpenFileDialog1.SafeFileName() not working? This returns only name and its extension.

And if you need to use Path class it has GetFileName() that also returns the file name and extension.
Was This Post Helpful? 1
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 04 October 2011 - 03:14 PM

View Posttrevster344, on 05 October 2011 - 03:16 AM, said:

I suggest you use these. :)

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim l As File
        l.Move(filepath, "C:\Users\Trevor\Desktop\VB.NET Programs\myFile" & filepath.Substring(filepath.LastIndexOf("."), 4))

    End Sub


Move() is shared, you have to use class name to reference it.

View Posttrevster344, on 05 October 2011 - 03:16 AM, said:

Also you can use string methods to break down your filepath, and find that extension, and filename.

For example:

        Dim filepath As String = "C:\trevsfile\koll.exe"
        Dim filepart1 As String = filepath.Substring(filepath.LastIndexOf("\") + 1)
        Dim file() As String = filepart1.Split(".")
        MessageBox.Show(file(0) & ", " & file(1))


Paths should be paths not string for better usage of them. So substring() or any string manipulation is not advised with paths. We use Path class instead
Was This Post Helpful? 2
  • +
  • -

#9 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,478
  • Joined: 16-March 11

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 04 October 2011 - 03:45 PM

Always learning something. :)
Was This Post Helpful? 1
  • +
  • -

#10 User_5573  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 12-November 10

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 05 October 2011 - 12:36 PM

Thank you all!
Once again the friendly community of Dream In Code has helped me through another obstacle.
I pride myself on replying with the answer so others that have the same problem may understand.

What I had to do was:
Dim fileName As String

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
   TextBox1.Text = OpenFileDialog1.FileName
   fileName = OpenFileDialog1.SafeFileName
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   If TextBox1.Text = "" Then
      MsgBox("No File Selected!")
   Else
      My.Computer.FileSystem.MoveFile(OpenFileDialog1.FileName, "C:\Dir\" & fileName)
   End If
End Sub



Using:
1 Button
1 TextBox
1 OpenFileDialog

The If statement is there to avoid an error if the button is clicked and there is no file selected.

Thank you again Dream In Code Community!
Was This Post Helpful? 2
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 05 October 2011 - 12:45 PM

Glad you get it working and thanks for sharing the result :)

EDIT: Also you can make button1 disabled until user select a file. Also you can check what user clicked from dialog box by checking the DialogResult. This is just addition you may think of it for your further usages.

This post has been edited by smohd: 05 October 2011 - 01:33 PM

Was This Post Helpful? 1
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4311
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 05 October 2011 - 05:53 PM

I very much suggest, as Adam has, not to just splice your path and filename together. Path.Combine is the method you want to use. It intelligently merges file paths (adds backslashes where necessary, etc...).
Was This Post Helpful? 0
  • +
  • -

#13 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,478
  • Joined: 16-March 11

Re: Attempting To Move OpenFileDialog Result To Directory

Posted 05 October 2011 - 07:32 PM

Wow that path class saved me a lot of time. I never even knew about it before lol.

Came up with this code real quick, did the job wonderfully lol.

        OpenFileDialog1.ShowDialog()

        Dim file As String = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)
        Dim extension As String = System.IO.Path.GetExtension(OpenFileDialog1.FileName)

        MessageBox.Show(file & ", " & extension)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1