在需要登录凭据的网络共享文件夹上使用Directory.getFiles



我当前正在尝试制作一个小型应用程序,该应用程序将图像从主要源文件夹分配到网络上计算机上的其他文件夹,但是所有其他计算机文件夹都需要我当前是我当前是登录凭据将它们存储在SQL中。

这是我所需的行:

fileCount = Directory.GetFiles(My.Settings.path212 + "", "*.jpg", SearchOption.TopDirectoryOnly).Length()

您可以看到,我需要在我要访问的每个文件夹中读取文件计数。目前,我在提供SQL中存储的凭据时找不到使用相同功能的方法。

更新#1:我没有提到由于域政策而无法使用映射驱动器。

好吧,我找到了我的解决方案,它是@pstrjds的从c#到vb.net givin的端口代码。只需要分享它。

在类中添加以下内容:

<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

相关内容

最新更新