-
May 16th, 2018, 07:40 PM
#1
Thread Starter
Fanatic Member
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
-
May 17th, 2018, 03:35 AM
#2
Re: CopyDirectory (User folders)
Put a try catch in there.
Also are you sure about drive "E:" you have windows on "E:"?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
May 17th, 2018, 07:08 AM
#3
Thread Starter
Fanatic Member
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"
-
May 17th, 2018, 09:59 AM
#4
Thread Starter
Fanatic Member
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)
-
May 17th, 2018, 10:46 AM
#5
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").
-
May 17th, 2018, 10:54 AM
#6
Thread Starter
Fanatic Member
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...
-
May 18th, 2018, 04:34 AM
#7
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
May 20th, 2018, 10:09 PM
#8
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|