开发者

VB.NET Copy Dirs into new Dir same Path, Error "process access being used by another process."

开发者 https://www.devze.com 2023-03-15 05:15 出处:网络
Here Is my code Public Sub MoveAllFolders(ByVal fromPathInfo As DirectoryInfo, ByVal toPath As String)

Here Is my code

Public Sub MoveAllFolders(ByVal fromPathInfo As DirectoryInfo, ByVal toPath As String)
   开发者_如何学运维 Dim toPathInfo = New DirectoryInfo(toPath)
    If (Not toPathInfo.Exists) Then
        toPathInfo.Create()
    End If
    'move all folders
    For Each dir As DirectoryInfo In fromPathInfo.GetDirectories()
        dir.MoveTo(Path.Combine(toPath, dir.Name))
    Next
End Sub

MoveAllFolders("D:\Users\TheUser!\Desktop\dd", "D:\Users\TheUser!\Desktop\dd\Folders)

My goal is to move all folder inside a folder into a folder named Folders. so If I do it on desktop all the folders in desktop will go to "Folders"

but I get an error "The process cannot access the file because it is being used by another process." so this code can't work this way, so is there any way to do what I wanna do?

Thanks alot!


You are moving your target-directoy into itself.

You could check if the destination-path contains the source-directory's FullName.

If Not toPath.Contains(fromPathInfo.FullName) Then
    dir.MoveTo(IO.Path.Combine(toPath, dir.Name))
 End If

But this method would be quite hacky. Consider a folder '"D:\abc1' and a folder '"D:\abc2'. Contains would return true in this case even if the folder "abc1" and "abc2" are not the same.

This should work better:

Public Sub MoveAllFolders(ByVal fromDir As IO.DirectoryInfo, ByVal toDir As IO.DirectoryInfo, Optional ByVal excludeList As List(Of String) = Nothing)
    If (Not toDir.Exists) Then
        toDir.Create()
    End If
    'move all folders
    For Each dir As IO.DirectoryInfo In fromDir.GetDirectories()
        Dim targetPath = IO.Path.Combine(toDir.FullName, dir.Name)
        If Not toDir.FullName = dir.FullName _
            AndAlso Not IsParentDirectory(toDir, dir) _
            AndAlso Not IO.Directory.Exists(targetPath) _
            AndAlso (excludeList Is Nothing _
              OrElse Not excludeList.Contains(dir.FullName, StringComparer.InvariantCultureIgnoreCase)) Then
            Try
                dir.MoveTo(targetPath)
            Catch ioEx As IO.IOException
                'ignore this directory'
            Catch authEx As UnauthorizedAccessException
                'ignore this directory'
            Catch ex As Exception
                Throw
            End Try
        End If
    Next
End Sub

Public Shared Function IsParentDirectory(ByVal subDir As IO.DirectoryInfo, ByVal parentDir As IO.DirectoryInfo) As Boolean
    Dim isParent As Boolean = False
    While subDir.Parent IsNot Nothing
        If subDir.Parent.FullName = parentDir.FullName Then
            isParent = True
            Exit While
        Else
            subDir = subDir.Parent
        End If
    End While
    Return isParent
End Function

You could use this function in this way:

Dim excludePathList As New List(Of String)
excludePathList.Add("C:\Temp\DoNotMoveMe1\")
excludePathList.Add("C:\Temp\DoNotMoveMe2\")
MoveAllFolders(New IO.DirectoryInfo("C:\Temp\"), New IO.DirectoryInfo("C:\Temp\temp-sub\"), excludePathList)

Edit: updated according to your last comment (untested).

0

精彩评论

暂无评论...
验证码 换一张
取 消