删除和移动文件

时间:2013-07-19 07:16:24

标签: vb.net file stream

我正在开发vb.net应用程序框架4,我有小问题 在上传文件之前,过程如下所示,我将检查文件是否已经存在然后我将删除它们然后上传新文件。只有在某些时候返回时才能正常工作错误该进程无法访问该文件,因为它正在被其他进程使用 这是我的代码

 Function MoveFiels(ByVal fn As String) As Boolean
        Dim flg As Boolean
        Application.DoEvents()
        Try
            If File.Exists("des" &  \fn) Then
                File.Delete("des" &  \fn)
                txtErrors.Text &= vbCrLf & "File Deleted and Replace will New File = " & fn & vbCrLf
                Application.DoEvents()
            End If
            System.Threading.Thread.Sleep(1000)
            File.Move("source" & \fn, "des" &  \fn)
            flg = True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return flg

1 个答案:

答案 0 :(得分:0)

为什么要睡觉线程

System.Threading.Thread.Sleep(1000)

? 我认为必须给你的应用程序留出时间来删除文件?也许有时候时间不够,这就是你得到错误的原因。 如果您只想等到文件被删除,可以尝试

While System.IO.File.Exists("des" &  \fn)
End While
File.Move("source" & \fn, "des" &  \fn)