我有一些代码可以通过LAN网络删除文件夹中的所有文件。 我无法弄清楚为什么,但它从来没有捕获异常显示错误它只会崩溃我已经从网络离线测试删除功能和部分代码工作正常(可以测试网络,直到明天回来工作)。有人知道我哪里出错吗?并且还有一种更好的方式来编写代码,因为它感觉马虎,因为它执行的简单功能太多了。 它的意思是清除默认临时目录的任何文件和子文件夹的3个临时文件夹上的时间文件。在Windows中,首先检查文本框以查看是否已输入工作站名称,如果没有,则检查下一个文本框中的IP代码如下:
Private Sub TempCleanButton_Click(sender As Object, e As EventArgs) Handles TempCleanButton.Click
If txtWS.Text = "" Then
ToolStripStatusLabel1.Text = "Deleting Temp Files Please Wait..."
' loop through each file in the target directory
For Each filePath As String In Directory.GetFiles("\\" & txtIP.Text & "\C$\Windows\Temp")
Try
'Delete the file if possible...otherwise skip it
File.Delete(filePath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each SubFolderPath As String In Directory.GetDirectories("\\" & txtIP.Text & "\C$\Windows\Temp")
' delete the sub-folder if possible...otherwise skip it
Try
Directory.Delete(SubFolderPath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each filePath As String In Directory.GetFiles("\\" & txtIP.Text & "\C$\Windows\Tmp")
Try
'Delete the file if possible...otherwise skip it
File.Delete(filePath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each SubFolderPath As String In Directory.GetDirectories("\\" & txtIP.Text & "\C$\Windows\Tmp")
' delete the sub-folder if possible...otherwise skip it
Try
Directory.Delete(SubFolderPath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each filePath As String In Directory.GetFiles("\\" & txtIP.Text & "C:\Users\Environment.UserName\AppData\Local\Temp")
Try
'Delete the file if possible...otherwise skip it
File.Delete(filePath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each SubFolderPath As String In Directory.GetDirectories("\\" & txtIP.Text & "C:\Users\Environment.UserName\AppData\Local\Temp")
' delete the sub-folder if possible...otherwise skip it
Try
Directory.Delete(SubFolderPath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
Else
ToolStripStatusLabel1.Text = "Deleting Temp Files Please Wait..."
' loop through each file in the target directory
For Each filePath As String In Directory.GetFiles("\\" & txtWS.Text & "\C$\Windows\Temp")
Try
'Delete the file if possible...otherwise skip it
File.Delete(filePath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each SubFolderPath As String In Directory.GetDirectories("\\" & txtWS.Text & "\C$\Windows\Temp")
' delete the sub-folder if possible...otherwise skip it
Try
Directory.Delete(SubFolderPath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each filePath As String In Directory.GetFiles("\\" & txtWS.Text & "\C$\Windows\Tmp")
Try
'Delete the file if possible...otherwise skip it
File.Delete(filePath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each SubFolderPath As String In Directory.GetDirectories("\\" & txtWS.Text & "\C$\Windows\Tmp")
' delete the sub-folder if possible...otherwise skip it
Try
Directory.Delete(SubFolderPath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each filePath As String In Directory.GetFiles("\\" & txtWS.Text & "C:\Users\Environment.UserName\AppData\Local\Temp")
Try
'Delete the file if possible...otherwise skip it
File.Delete(filePath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
' loop through each file in the target directory
For Each SubFolderPath As String In Directory.GetDirectories("\\" & txtWS.Text & "C:\Users\Environment.UserName\AppData\Local\Temp")
' delete the sub-folder if possible...otherwise skip it
Try
Directory.Delete(SubFolderPath)
Catch ex As Exception
MsgBox("Unable to Delete: " & vbCrLf & "Error: " & ex.Message, MsgBoxStyle.Critical)
End Try
Next
End If
End Sub