Muh*_*ana 0 vb.net visual-studio-2010
只要该文件尚不存在,以下代码就会移动文件.如果是,则不会移动文件.
我的问题是关于File.Move.msgbox何时显示?文件完全移动后会显示,还是在File.Move执行完毕后显示.
根据文件大小,移动文件可能需要一段时间,因此我不希望msgbox显示,直到文件完全移动.
有没有更好的方法呢?
For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Temp\", FileIO.SearchOption.SearchAllSubDirectories, "*.zip")
Dim foundFileInfo As New System.IO.FileInfo(foundFile)
If My.Computer.FileSystem.FileExists("C:\Transfer\" & foundFileInfo.Name) Then
Msgbox("File already exists and will not moved!")
Exit Sub
Else
File.Move(foundFile, "C:\Transfer\" & foundFileInfo.Name)
Msgbox("File has been moved!")
End If
Next
Run Code Online (Sandbox Code Playgroud)
根据此来源,File.Move调用是同步的,这意味着只有在文件移动后才会显示您的msgbox,无论其大小如何.
为了完整起见,如果您不想阻止UI,可以尝试以下方法:
' This must be placed outside your sub/function
Delegate Sub MoveDelegate(iSrc As String, iDest As String)
' This line and the following go inside your sub/function
Dim f As MoveDelegate = AddressOf File.Move
' Call File.Move asynchronously
f.BeginInvoke(
foundFile,
"C:\Transfer\" & foundFile,
New AsyncCallback(Sub(r As IAsyncResult)
' this code is executed when the move is complete
MsgBox("File has been moved!")
End Sub), Nothing)
Run Code Online (Sandbox Code Playgroud)
或者您可以探索新的异步/等待指令.
| 归档时间: |
|
| 查看次数: |
1001 次 |
| 最近记录: |