重命名文件时出错 "Could not locate part of the path"

Error "Could not locate part of the path" when renaming file

我验证了所有目录路径都存在,所以我在这里完全不知所措。

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.FullName 的关键信息。这是展示这一点的片段。

' 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

这是一张显示上次尝试结果的照片。 33个文件中,有7个文件重命名失败:

这里的目标是重命名我们在无人机飞行期间创建的图像文件。这些航班是静态的,相同 GPS 位置的相同数量的图像,因此我们希望能够引用任何现有的航班图像,以便我们可以提取文件名。文件名的唯一动态部分是前 8 个字符(日期:yyyymmDD)。

原来是部分文件名太长无法重命名。

为了解决文件名长度超过 MAX_PATH 的问题,我使用了以下线程:

解析文件名长于 MAX_PATH 异常 > Forum
处理文件名长于 MAX_PATH > Forum ... [ specifically the answer by Wolf5 ]