Search code examples
vb.netuser-permissionsfile-copying

VB.Net - "My.Computer.FileSystem.CopyDirectory" Does Not Copy Directory or Permissions of Directories Inside Target Directory


I tried to be as descriptive in my title as possible, but to clarify I have a VB.Net application and I'm attempting to copy a directory to another machine with file permissions and all child files/folders.

Using "My.Computer.FileSystem.CopyDirectory", the directory that is chosen to be copied does NOT get copied over like the MSDN implies; but instead the contents of the directory are copied to the destination directory. If there is a folder inside of that directory, the folder is copied but the permissions of said folder are NOT. Both of these things pose a huge issue because it's imperative that the original folder's permissions, and any other folders inside of the original folder, get copied along with it. This is possible using PowerShell, is it possible using VB.Net?

Thanks in advance.

For Each item As String In stations
            copyTo = stations([i].ToString)

            If IsHostAvailable(copyTo) Then
                LogBreak()
                LogOutput(TimeStamp() + ": " + copyTo + " available. Beginning file push...")
                copyTo = "\\"
                copyTo = Path.Combine(copyTo, stations([i].ToString))
                copyToLoc1 = copyTo.ToString
                copyToLoc1 = Path.Combine(copyTo, pushLocationBox1.ToString.Remove(0, 36))

                LogBreak()
                LogOutput(TimeStamp() + ": " + "Coyping- " + "\n" + pushFrom1 + "\n" + "...to station '" + copyTo + "'.")

                If (File.Exists(pushFrom1) AndAlso (System.IO.Directory.Exists(copyToLoc1))) Then
                    Dim pushFileName As String = Path.GetFileName(pushFrom1)
                    My.Computer.FileSystem.CopyFile(pushFrom1, Path.Combine(copyToLoc1, pushFileName), True)
                    LogOutput(TimeStamp() + ": " + "File 1 copied.")
                ElseIf (File.Exists(pushFrom1) AndAlso (System.IO.Directory.Exists(copyToLoc1) = False)) Then
                    Directory.CreateDirectory(copyToLoc1)
                    LogOutput(TimeStamp() + ": " + "Directory created.")
                    My.Computer.FileSystem.CopyFile(pushFrom1, copyToLoc1, True)
                    LogOutput(TimeStamp() + ": " + "File 1 copied.")
                ElseIf (Directory.Exists(pushFrom1)) Then
                    My.Computer.FileSystem.CopyDirectory(pushFrom1, copyToLoc1, True)
                    Dim srcPerms As New FileInfo(pushFrom1)
                    Dim destPerms As New FileInfo(Path.Combine(copyToLoc1, pushFrom1))
                    Dim permissions As FileSecurity = srcPerms.GetAccessControl()
                    permissions.SetAccessRuleProtection(True, True)
                    destPerms.SetAccessControl(permissions)
                    LogOutput(TimeStamp() + ": " + "Directory 1 copied.")
                Else
                    LogOutput(TimeStamp() + ": " + "The file or directory selected to be copied can no longer be found. (#1)")
                    MsgBox("The file or directory selected to be copied can no longer be found. (#1)", MsgBoxStyle.Critical, "Error!")
                End If
            Else
                LogOutput(TimeStamp() + ": " + "Ping request timed out on " + copyTo + ". Moving to next station...")
                FailOutput(copyTo)
            End If
            i += 1
        Next

Solution

  • i think the directory path in copyToLoc1 is not correct, i tested code below and it works.

    My.Computer.FileSystem.CopyDirectory("E:\Users\test1\Desktop\test", "E:\Users\test1\Desktop\test2", True)
    

    Your program might not be storing info to copyToLoc1 correctly to debug and see if its the case

    comment out this code

    'My.Computer.FileSystem.CopyDirectory(pushFrom1, copyToLoc1, True)
    

    add the following code

    MsgBox(copyToLoc1)
    

    You would get a msg with the new directory you trying to create.

    see if its the correct directory you are trying to create.