views:

151

answers:

2

Hi,

I have a treeview control which is being used to browse various folder on the users system drive. It can also be set up to connect network folders using UNC paths e.g "\server\files". At the moment I'm getting a list of Directories by using My.Computer.FileSystem.GetDirectories however this obviously fails when the folder is password protected.

Is there a way that I can get Windows to open the credentials dialog as you would see in Windows Explorer and deal with the credentials side so then the user name & password is cached as well?

I'm not allowed to physically map drives by the way.

Thanks for any help.

Rob

A: 

You will need to use P/Invoke to call a Windows API function, specifically WNetAddConnection3.

See the answer to my question here: http://stackoverflow.com/questions/379941/can-i-force-windows-to-challenge-a-user-for-authentication

Ch00k
A: 

Hi Ch00k,

I tried this but I keep getting an exception Unable to find an entry point named 'WNetAddConnection3' in DLL 'mpr.dll'. on the WNetAddConnection3 function call.

This is my code

    Declare Function WNetAddConnection3 Lib "mpr.dll" Alias "WNetAddConnection3" (ByVal hWndOwner As IntPtr, ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
Private Const RESOURCETYPE_ANY As Integer = 0
Private Const CONNECT_INTERACTIVE As Integer = 8
Private Const CONNECT_PROMPT As Integer = 10
Public Structure NETRESOURCE
    Public dwScope As Integer
    Public dwType As Integer
    Public dwDisplayType As Integer
    Public dwUsage As Integer
    Public LocalName As String
    Public RemoteName As String
    Public Comment As String
    Public Provider As String
End Structure


Dim pNr As New NETRESOURCE
pNr.dwType = RESOURCETYPE_ANY
pNr.RemoteName = "\\Server\"
WNetAddConnection3(Me.Handle, pNr, Nothing, Nothing, CONNECT_INTERACTIVE)

Cheers, Rob

rob
Actually I've solved it the alias should have been WNetAddConnection3A as I should have been using the ANSI name.
rob