我有一个模拟课程,到目前为止在Windows XP上工作得很好,但是当我尝试在Windows 7上使用相同的模拟时,我遇到了一个问题。据我所知,模仿有效,但当我尝试访问此用户下的文件时,我得到访问被拒绝错误。有没有其他人遇到这个问题并找到了解决办法?
Public Sub BeginImpersonation()
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Const SecurityImpersonation As Integer = 2
Dim win32ErrorNumber As Integer
_tokenHandle = IntPtr.Zero
_dupeTokenHandle = IntPtr.Zero
If Not LogonUser(_username, _domainname, _password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, _tokenHandle) Then
win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
Throw New ImpersonationException(win32ErrorNumber, GetErrorMessage(win32ErrorNumber), _username, _domainname)
End If
If Not DuplicateToken(_tokenHandle, SecurityImpersonation, _dupeTokenHandle) Then
win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
CloseHandle(_tokenHandle)
Throw New ImpersonationException(win32ErrorNumber, "Unable to duplicate token!", _username, _domainname)
End If
Dim newId As New System.Security.Principal.WindowsIdentity(_dupeTokenHandle)
_impersonatedUser = newId.Impersonate()
End Sub
Dim aa As fcGlobals.AliasAccount
'Use Impersonation
aa = New fcGlobals.AliasAccount("xxxx", "xxxx")
aa.BeginImpersonation()
For a = 0 To mPages.Count - 1
If System.IO.File.Exists(mFileNames(a)) = True Then
System.IO.File.Delete(mFileNames(a))
End If
Next
aa.EndImpersonation()
aa.Dispose()