2015-10-13 35 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