CyronDnB
CyronDnB

Reputation: 35

Visual Studio 2013 - Move File To Another Path

Good-afternoon all,

I am trying to create this code that will allow me to create a new folder in the directory from what I enter into TextBox1.Text, then open a dialog box and select a PDF which will then put the files path into TextBox2 (the same applies for a separate PDF in TextBox3).

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

Additional information: Could not complete operation since a directory already exists in this path '\\ANVILSRV\Public\Completed Works Orders\98789'.       

-

This is the error that I am getting when trying to complete the operation, it creates the folder and does not move any files.

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    Dim txt As String
    txt = TextBox1.Text

    If My.Computer.FileSystem.DirectoryExists("\\ANVILSRV\Public\Completed Works Orders\" & txt & "") Then

        MsgBox("Could not create the folder " & txt & " because it already exists.")

    Else

        My.Computer.FileSystem.CreateDirectory("\\ANVILSRV\Public\Completed Works Orders\" & txt & "")

        My.Computer.FileSystem.MoveFile(TextBox2.Text, ("\\ANVILSRV\Public\Completed Works Orders\" & txt & ""), True)

        My.Computer.FileSystem.MoveFile(TextBox3.Text, ("\\ANVILSRV\Public\Completed Works Orders\" & txt & ""), True)

    End If

End Sub

Any advice or help is much appreciated.

Thanks,

Stephen

Upvotes: 2

Views: 1469

Answers (2)

Kannan Suresh
Kannan Suresh

Reputation: 4580

Move file syntax needs the following arguments

sourceFileName = full path to the source file

destinationFileName = full path to the destination file

overWrite = boolean value which specifies whether to overwrite the destination file if it already exist

FileSystem.MoveFile(sourceFileName As String, destinationFileName As String, overWrite As Boolean)

In your code, instead of giving the full file path for the argument destinationFileName you specified the folder path. Give the full file name in your code and it will work. For example "C:\Windows\DirectX.txt"

Try the following code

My.Computer.FileSystem.MoveFile(TextBox2.Text, ("\\ANVILSRV\Public\Completed Works Orders\" & txt & "\" & fileName), True)

Upvotes: 2

Lews Therin
Lews Therin

Reputation: 3777

I would recommend changing the path that you reuse a few times in your code into a Constant.

Also, the "" you have at the end of all your path strings is not needed. Here's what I mean (I only made a quick test so I didn't include everything, but you can get the idea from this; I tested the following code and it works):

Const path As String = "\\ANVILSRV\Public\Completed Works Orders\"

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim txt As String
    txt = TextBox1.Text

    If My.Computer.FileSystem.DirectoryExists(path & txt) Then
        MsgBox("Could not create the folder " & txt & " because it already exists.")
    Else
        My.Computer.FileSystem.CreateDirectory(path & txt)
    End If
End Sub

Upvotes: 0

Related Questions