Vb.net visualbasic中的安全复制文件
下面的代码是从C#代码中删除的。我想知道……的重要性是什么。。。。代码中的…语句Vb.net visualbasic中的安全复制文件,vb.net,Vb.net,下面的代码是从C#代码中删除的。我想知道……的重要性是什么。。。。代码中的…语句 Public Sub CopyFile(sourceFile As String, backupFile As String, overwrite As Boolean) If String.IsNullOrEmpty(sourceFile) Then Throw New ArgumentNullException("sourceFile") End If If String.IsNullOrEmpty
Public Sub CopyFile(sourceFile As String, backupFile As String, overwrite As Boolean)
If String.IsNullOrEmpty(sourceFile) Then
Throw New ArgumentNullException("sourceFile")
End If
If String.IsNullOrEmpty(backupFile) Then
Throw New ArgumentNullException("backupFile")
End If
' According to MSDN Certain file attributes, Hidden and ReadOnly, can be combined. Other attributes, such as Normal, must be used alone.
If File.Exists(backupFile) Then
File.SetAttributes(backupFile, FileAttributes.Normal)
End If
' ????????
For index As Integer = 0 To 9
Try
File.Copy(sourceFile, backupFile, overwrite)
Exit Try
Catch ex As FileNotFoundException
Throw
Catch ex As Exception
If Not overwrite Then
Throw
End If
If index = 9 Then
Throw
End If
Thread.Sleep(1000)
End Try
Next
End Sub
看起来它正在设置重试循环-尝试10次,如果它不起作用并且不是另一个错误(未找到文件,或者
overwrite
为false的异常),则抛出异常。可能正在尝试访问被另一个进程锁定的文件。另一方面,您可以使用VB.Net的条件异常处理稍微简化此代码(…Catch ex As exception When overwrite and also index<9
…)。