好吧,我发现我的解决方案及其在C#中的移植代码vb.net由@pstrjds得到安宁。并在需要时分享。
添加以下的类:
<DllImport("advapi32.dll", SetLastError := True)> _
Private Shared Function LogonUser(lpszUsername As String, lpszDomain As String, lpszPassword As String, dwLogonType As Integer, dwLogonProvider As Integer, ByRef phToken As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll")> _
Private Shared Function CloseHandle(hObject As IntPtr) As [Boolean]
End Function
这里是用法:
Dim token As IntPtr = IntPtr.Zero
LogonUser("username", "remotemachine-name/ipaddress", "password", 9, 0, token)
Using person As WindowsImpersonationContext = New WindowsIdentity(token).Impersonate()
Try
fileCount = Directory.GetFiles(My.Settings.path212 + "\", "*.jpg", SearchOption.TopDirectoryOnly).Length()
Catch ex As IOException
MsgBox(ex.Message)
Finally
person.Undo()
CloseHandle(token)
End Try
End Using
你是什么意思?代码正常工作。 – Subaz
你看过这些问题吗?他们应该指出你正确的方向:https://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting -to-a-network-share和https://stackoverflow.com/questions/2563724/accessing-password-protected-network-drives-in-windows-in-c – pstrjds
这是另一个可能有帮助的非现场资源。它也在C#中,但应该是相当简单的移植到VB - http://www.mobilemotion.eu/?p=1582 – pstrjds