dcsimg
Results 1 to 8 of 8
  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    418

    CopyDirectory (User folders)

    Ive created a little code to keep a 3 day archive of specified directories, I am not sure why the Users directory wont copy. Most likely a security problem, when running the code as administrator there are no exceptions and no files are copied. If not elevated it will throw "UnauthorizedAccessException" which is expected.

    Code:
    Module ModuleSync
        Public Sub Main()
            Dim SourceFolders As New List(Of String)()
            With SourceFolders
                .Add("C:\Users")
            End With
    
            Dim Drive As String = "E:"
            If IO.Directory.Exists(Drive) Then
                Dim DestinationRoot As String = Drive & "\SYNCED"
                If Not IO.Directory.Exists(DestinationRoot) Then
                    IO.Directory.CreateDirectory(DestinationRoot)
                End If
                Dim DestinationRemove As String = DestinationRoot & "\" & Date.Today.AddDays(-3).ToString("yyyy-MM-dd")
                DestinationRoot &= "\" & Date.Today.ToString("yyyy-MM-dd")
    
                If Not IO.Directory.Exists(DestinationRoot) Then
                    IO.Directory.CreateDirectory(DestinationRoot)
                End If
    
                For Each Str As String In SourceFolders
                    My.Computer.FileSystem.CopyDirectory(Str, DestinationRoot, True) 'Nothing is copied
                Next
    
                If IO.Directory.Exists(DestinationRemove) Then
                    IO.Directory.Delete(DestinationRemove, True)
                End If
            End If
        End Sub
    End Module

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,263

    Re: CopyDirectory (User folders)

    Put a try catch in there.
    Also are you sure about drive "E:" you have windows on "E:"?
    Slow as hell.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    418

    Re: CopyDirectory (User folders)

    I dont think try block will catch anything as there is no exception.

    E: is the destination. The source folders are in the list "SourceFolders"

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    418

    Re: CopyDirectory (User folders)

    Ok, so playing with it some more at work..
    if you drop the overwrite arg, show the UI, and do not run as administrator it will prompt for administrator access. I assume if you are actually logged in as administrator it should not prompt.

    This will prompt for admin access and perform the copy procedure once supplied.
    Code:
     FileIO.FileSystem.CopyDirectory("C:\Users\", NewFolderName, showUI:=FileIO.UIOption.AllDialogs)
    This will not throw an exception, show the UI, or copy the files to destination. Scratching my head...
    Code:
     FileIO.FileSystem.CopyDirectory("C:\Users\", NewFolderName, True, FileIO.UIOption.AllDialogs)

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,813

    Re: CopyDirectory (User folders)

    Being logged in with an account that has administrator rights is not the same as running it as an administrator.

    The security features built in to Windows mean that programs do not get admin rights unless the user explicitly allows it (this helps stop malware doing nasty things). That can be via a prompt like you added, or by the program explicitly asking for elevation (which shows a prompt and restarts the program), or by the user asking to start the program with admin rights (usually by right-clicking then "run as administrator").

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    418

    Re: CopyDirectory (User folders)

    Si, I get all that. Running the app as administrator nothing happens at all. Running the app logged in as administrator without the UI dialog, nothing happens at all(last night test). It's like you need to have the UI dialog or no show. I wont be able to see the result of showing UI when logged in as admin till I get home. I have a suspicion that during the copy some other dialog prompts will arise which would defeat the unattended procedure I am trying to achieve.

    About to resort to xcopy...

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,263

    Re: CopyDirectory (User folders)

    OK.
    I did not have any VS when I wrote this, now at work I gave it a more thorough look and I can see what you mean by access denied.
    The below tested at work, fixed your issue but when you run it you will get a "path too long" but that is an issue you must fix on .CopyDirectory and I will not get into this.
    So:

    Code:
    Imports System.Runtime.InteropServices
    Imports System.Security.Principal
    
    Module Module1
    
        Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal un As String, ByVal domain As String, ByVal pw As String, ByVal LogonType As Integer, ByVal LogonProvider As Integer, ByRef Token As IntPtr) As Boolean
    
        Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean
    
     Sub Main()
            Dim SourceFolders As New List(Of String)()
            With SourceFolders
                .Add("C:\Users")
            End With
            Dim tokenHandle As New IntPtr(0)
    
    ' GIVE YOUR CREDENTIALS
               If LogonUser("username", "domain", "password", 3, 0, tokenHandle) Then
                Dim newId As New WindowsIdentity(tokenHandle)
                Using impersonatedUser As WindowsImpersonationContext = newId.Impersonate()
    
                    Dim Drive As String = "E:"
                    If IO.Directory.Exists(Drive) Then
                        Dim DestinationRoot As String = Drive & "\SYNCED"
                        If Not IO.Directory.Exists(DestinationRoot) Then
                            IO.Directory.CreateDirectory(DestinationRoot)
                        End If
                        Dim DestinationRemove As String = DestinationRoot & "\" & Date.Today.AddDays(-3).ToString("yyyy-MM-dd")
                        DestinationRoot &= "\" & Date.Today.ToString("yyyy-MM-dd")
    
                        If Not IO.Directory.Exists(DestinationRoot) Then
                            IO.Directory.CreateDirectory(DestinationRoot)
                        End If
    
                        For Each Str As String In SourceFolders
                            My.Computer.FileSystem.CopyDirectory(Str, DestinationRoot, True) 'TOFIX -WORKS with another file too long error
                        Next
    
                        If IO.Directory.Exists(DestinationRemove) Then
                            IO.Directory.Delete(DestinationRemove, True)
                        End If
                    End If
                End Using
                 CloseHandle(tokenHandle)
            End If
        End Sub
    
    End Module
    Last edited by sapator; May 18th, 2018 at 04:51 AM.
    Slow as hell.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    418

    Re: CopyDirectory (User folders)

    thanks Sap, but this leaves an ill taste in my brain. I am going to just use xcopy via batch scripting

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.