1

I am currently trying to make a small application that distributes images from the main source folder to other folders on computers on the network, but all the other computers folders need login credentials which I am currently storing them in SQL.

here is a look over my needed line:

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

As you can see I need to read the files count in each folder I am accessing. And currently, i cannot find a way to use the same function while providing the credentials stored in SQL.

Update#1: I didnt mention that maping drives cannot be used because of domain policy.

Anwar Fazza
  • 35
  • 1
  • 9
  • what do you mean? the code works correctly tho. – Subaz Aug 29 '17 at 06:04
  • 1
    Have you taken a look at either of these questions, they should point you in the right direction: https://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting-to-a-network-share and https://stackoverflow.com/questions/2563724/accessing-password-protected-network-drives-in-windows-in-c – pstrjds Aug 29 '17 at 06:06
  • This is another offsite resource that may be of help. It is also in C# but should be pretty straightforward to port to VB - http://www.mobilemotion.eu/?p=1582 – pstrjds Aug 29 '17 at 06:16
  • Possible duplicate of [How to open a password protected shared network folder using VB.NET?](https://stackoverflow.com/questions/321128/how-to-open-a-password-protected-shared-network-folder-using-vb-net) – pstrjds Aug 29 '17 at 06:18
  • i forgot to mention @pstrjds Drive Maping is not possible due to domain policy. So people who will use this small application are not allowed to do such action. – Anwar Fazza Aug 29 '17 at 06:25

1 Answers1

1

Well i have found my solution its a ported code from C# to vb.net givin by @pstrjds. and just sharing it incase needed.

Add the following in the class:

<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

And here is the usage :

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
Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38
Anwar Fazza
  • 35
  • 1
  • 9