VB.NET调用FileInfo.MoveTo重命名文件报路径找不到错误如何解决?
无人机飞行影像重命名工具路径异常排查
问题现象
开发无人机飞行影像文件重命名工具时,调用FileInfo.MoveTo方法执行重命名操作时,抛出「Could not locate part of the path」错误,已核实所有目录路径均存在,但33个待处理文件中仍有7个重命名失败。
原始核心代码
For Each i As Project.ImageryCaptureTask.FileProperties In t.FileCollection Dim NewFilename As New FileInfo(Path.Combine(Root.FullName, i.LVItem.Text)) If NewFilename.Exists Then ' ...Things get done Else Try i.FI.MoveTo(NewFilename.FullName) ' <- Error thrown here (obviously) Catch IOEx As IOException MsgBox(IOEx.Message & vbCr & vbCr & "Error renaming to " & NewFilename.FullName, vbOKOnly, IOEx.Source) Catch Ex As Exception MsgBox(Ex.Message & vbCr & vbCr & "Error renaming to " & NewFilename.FullName, vbOKOnly, IOEx.Source) End Try End If Next
补充逻辑(Root目录生成规则)
' P.MainFolder.FullName is the DirectoryInfo instantiated from the FolderBrowserDialog If Directory.Exists(P.MainFolder.FullName) Then For Each t As Project.ImageryCaptureTask In P.TaskCollection Dim SubFolder3DMapping As New DirectoryInfo(Path.Combine(P.MainFolder.FullName, (Val(t.TaskCreationDate) + t.OffsetDate).ToString & "_" & P.ProjectNumber & "_" & P.ProjectName & "_" & Project.ImageryCaptureTask.CapturingMethods.Aerial3DMapping.ToString.Insert(6, "_"))) Dim SubFolderPhotos As New DirectoryInfo(Path.Combine(P.MainFolder.FullName, (Val(t.TaskCreationDate) + t.OffsetDate).ToString & "_" & P.ProjectNumber & "_" & P.ProjectName & "_" & Project.ImageryCaptureTask.CapturingMethods.AerialPhotos.ToString.Insert(6, "_"))) Dim SubFolderVideos As New DirectoryInfo(Path.Combine(P.MainFolder.FullName, (Val(t.TaskCreationDate) + t.OffsetDate).ToString & "_" & P.ProjectNumber & "_" & P.ProjectName & "_" & Project.ImageryCaptureTask.CapturingMethods.AerialVideos.ToString.Insert(6, "_"))) If Not SubFolder3DMapping.Exists Then SubFolder3DMapping.Create() If Not SubFolderPhotos.Exists Then SubFolderPhotos.Create() If Not SubFolderVideos.Exists Then SubFolderVideos.Create() Dim Root As DirectoryInfo = Nothing Select Case True Case t.CaptureMethod = Project.ImageryCaptureTask.CapturingMethods.Aerial3DMapping Root = SubFolder3DMapping Case t.CaptureMethod = Project.ImageryCaptureTask.CapturingMethods.AerialPhotos Root = SubFolderPhotos Case t.CaptureMethod = Project.ImageryCaptureTask.CapturingMethods.AerialVideos Root = SubFolderVideos End Select For Each i As Project.ImageryCaptureTask.FileProperties In t.FileCollection Dim NewFilename As New FileInfo(Path.Combine(Root.FullName, i.LVItem.Text)) If NewFilename.Exists Then ' This should not happen, but if it does it gets handled here. Else Try i.FI.MoveTo(NewFilename.FullName) Catch IOEx As IOException MsgBox(IOEx.Message & vbCr & vbCr & "Error renaming to " & NewFilename.FullName, vbOKOnly, IOEx.Source) Catch Ex As Exception MsgBox(Ex.Message) End Try End If Next Next End If
业务需求说明
重命名无人机飞行生成的影像文件,飞行任务为固定配置,相同GPS点位生成的影像数量一致,文件名仅前8位(日期格式yyyymmDD)为动态字段,需要引用已有飞行影像提取对应文件名。
排查&解决方向
- 非法字符校验:检查7个失败文件对应的
i.LVItem.Text是否包含Windows路径不支持的特殊字符(< > : " / \ | ? *),这些字符会导致拼接后的路径实际无效,哪怕父目录存在也会触发路径找不到错误,建议新增非法字符替换逻辑后再拼接路径。 - 路径长度校验:Windows默认限制单条路径最长为260字符,若拼接后的
NewFilename.FullName长度超出上限也会触发该错误,可以在报错时输出路径长度确认,若确为长度问题可开启系统长路径支持,或优化子目录命名规则缩短路径长度。 - 加重试逻辑:无人机影像文件刚生成完成时可能被生成进程、杀毒软件临时锁定,也会抛出类路径错误,可新增重试逻辑,单次失败后等待100ms重试,最多重试3次。
- 兜底枚举匹配:现有
Select Case未加Case Else兜底逻辑,若任务的CaptureMethod不在现有枚举范围内会导致Root为Nothing,拼接路径直接失效,建议新增兜底分支,匹配失败时抛出明确的枚举不支持错误,避免隐式路径异常。
内容的提问来源于stack exchange,提问作者John89
相关产品推荐
相关产品推荐

