I am trying to Impersonate different users in order to find differences between two directories. This is my Code, but i get Access denied when the query executes. Notice that the users are correct and the above impersonation works when both DirectoryInfo executes.
Dim impersonateUser As New UserImpersonation
impersonateUser.impersonateUser("user1", "", "password1")
Dim dir1 = New DirectoryInfo("\\10.11.11.122\OnDemand Recordings")
impersonateUser.undoimpersonateUser()
impersonateUser.impersonateUser("user2", "", "password2")
Dim dir2 = New DirectoryInfo("\\10.11.11.172\OCDialer_Recordings\OnDemand")
impersonateUser.undoimpersonateUser()
'In this Block i get access denied
Dim filesinboth = From f1 In dir1.EnumerateFiles(System.IO.SearchOption.AllDirectories)
Join f2 In dir2.EnumerateFiles(System.IO.SearchOption.AllDirectories) On f1.Name Equals f2.Name
Select f1.Name
That impersonation probably sets credentials for the current thread, and that’s why you get the access-denied error – as the 2nd impersonation overwrites the 1st (and you can’t have more than one user running a single thread/app at the same time).
Try this:
This will work only if you are comparing relatively small amount of information about each file/dir (e.g. their names). Read [full] paths to directories and files into a list or some sort of tree-like/recursive collection (you can create something like the class below for the latter).
Or simply store full paths to
List<string>(same case for easier comparison).Then, read the complete directories separately. Compare them later. Store differences in 3rd list.