本文介绍了移动文件时的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经环顾网络,但找不到任何可以帮助我的东西。 我能够获得一些代码将一些文件从一个目录移到 另一个,工作正常: ======================== ============ Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button1.Click 尝试 Dim SourceDir As String =" C:\ test \" Dim DestinationDir As String = " \\\\\\\\\\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 对于i as Integer = 0到UBound(f) System.IO.File.Move(f(i),DestinationDir&" \"& ; FileNameWithoutThePath(f(i))) 下一页i Catch ex As Exception MsgBox(ex.Message.ToString,MsgBoxStyle.Critical," Error!") 结束尝试 结束子 公共函数FileNameWithoutThePath(ByVal b As String)As String Dim j As Int16 j = Convert.ToInt16(b.LastIndexOf(" \")) 返回b.Substring(j + 1) 结束函数 ==================================== 我遇到的问题是文件已存在于目标 目录中,或者它正在尝试移动的文件是否正在使用(已锁定)且错误 发生。我需要能够跳过问题文件并转到下一个要移动的文件。我已经尝试了各种各样的东西,比如在代码的各个部分递增i(i = i +1) 但是这没有用。 Any我将不胜感激! TIA, JimI''ve looked around the web but can''t find anything to help me out.I was able to get some code to move some files from one directory toanother, which works fine:====================================Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.ClickTryDim SourceDir As String = "C:\test\"Dim DestinationDir As String = "\\server1\share\test\"Dim f() As String = System.IO.Directory.GetFiles(SourceDir)For i As Integer = 0 To UBound(f)System.IO.File.Move(f(i), DestinationDir & "\" &FileNameWithoutThePath(f(i)))Next iCatch ex As ExceptionMsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error!")End TryEnd SubPublic Function FileNameWithoutThePath(ByVal b As String) As StringDim j As Int16j = Convert.ToInt16(b.LastIndexOf("\"))Return b.Substring(j + 1)End Function====================================The problem I run into is when the file already exists in the destinationdirectory or if the file it''s trying to move is in use (locked) and erroroccurs. I need to be able to skip the problem file and move on to the nextfile to be moved. I have tried various things like incrementing i (i = i +1)in various parts of the code but this hasn''t worked.Any assistance or direction would be appreciated!TIA,Jim推荐答案 这篇关于移动文件时的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!