2015-10-13 30 views
1

下午好一切,的Visual Studio 2013 - 移動文件到其他路徑

我試圖創建此代碼,讓我從我進入TextBox1.Text在目錄中創建一個新的文件夾,然後打開一個對話框,然後選擇一個PDF,然後將文件路徑放入TextBox2(TextBox3中的單獨PDF也是如此)。

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'.  

-

這是試圖完成操作時,我得到的錯誤,它創建的文件夾,不會移動任何文件。

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 

任何意見或幫助,非常感謝。

謝謝,

斯蒂芬

+0

我覺得TextBox1中和2將需要完全合格的路徑。如果目錄已經存在,奇怪的「附加信息」消息與CreateDirectory()不會引發錯誤。檢查存在可能是很好的做法,但不是必需的。文檔聲明IOException錯誤與權限有關,而不是存在。如果from路徑不完全限定會導致IOException。 – rheitzman

+0

我想我看到了問題 - from和to應該是完全限定的'filenames'。 To不是一個文件夾,而是一個完全合格的路徑。 – rheitzman

回答

1

移動文件的語法需要以下參數

sourceFileName =完整路徑源文件

destinationFileName =完整路徑到目標文件

overWrite =指定是否覆蓋t的布爾值如果它已經存在

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

在你的代碼,而不是給完整的文件路徑,參數destinationFileName您指定的文件夾路徑,他的目標文件。在你的代碼中給出完整的文件名稱,它將起作用。例如"C:\Windows\DirectX.txt"

嘗試下面的代碼

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

我建議改變你重用你的代碼幾次入常的路徑。

另外,""您不必使用所有路徑字符串的末尾。這裏就是我的意思(我只是做了一個簡單的測試,所以我不包括一切,但你可以從這個想法,我測試下面的代碼和它的作品):

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