Copy a file to another directory with OpenFileDialog?


  • I am new to VB and i am making a program where you can use the OpenFileDialog component to copy a file from its location to another.

    Whenever i select my file and press ok an error message pops up reading:

    "An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in Microsoft.VisualBasic.dll

    Additional information: Could not find a part of the path '(file destination)'."

    Here is my code:

        Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
            Dim OFD As New OpenFileDialog
            OFD.Filter = "Valve package (*.vpk)|*.vpk"
            If OFD.ShowDialog() = Windows.Forms.DialogResult.OK Then
                FileCopy(OFD.FileName, "DESTINATION")
            End If
        End Sub

    If somebody can help that would be awesome, thanks :)

    • Edited by GamblerUS Saturday, December 5, 2015 6:11 AM
    Saturday, December 5, 2015 5:25 AM


  • Probably you should create a full path to the destination file. Use OFD.FileName value and make the destination path using various functions of Path class, such as Path.GetPathRoot, Path.GetFileName, Path.Concat. You should obtain a string like “C:\SomeFolder\DESTINATION\SomeFile.vpk”. If the destination directory must be created, then consider Directory.Create.

    Saturday, December 5, 2015 8:10 AM