I did a Google Search in this Newsgroup for WNetAddConnection but
only found this thread, where and how do I search in the archives ?
Going down a different path shelling out to another program on the server
using Stored Proc xp_cmdshell, but I am sure something like this will come
up
again.
I did found the declarations for the Functions and a Class to find existing
connections. I'm not familiar with P\Invoke have heard it mentioned though,
is it only used for the C family of languages ?
Well here is what I found in VB.Net for anyone interested.
Declare Function WNetAddConnection Lib "mpr.dll" Alias
"WNetAddConnectionA"(ByVal lpszNetPath As String, ByVal lpszPassword As
String, ByVal lpszLocalName As String) As Integer
Declare Function WNetAddConnection2 Lib "mpr.dll" Alias
"WNetAddConnection2A"(ByRef lpNetResource As NETRESOURCE, ByVal lpPassword
As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
Public Class clsTestWNetGetConnection
#Region "Costanti"
Private Const DRIVE_UNKNOWN = 0
Private Const DRIVE_ABSENT = 1
Private Const DRIVE_REMOVABLE = 2
Private Const DRIVE_FIXED = 3
Private Const DRIVE_REMOTE = 4
Private Const DRIVE_CDROM = 5
Private Const DRIVE_RAMDISK = 6
' returns errors for UNC Path
Private Const ERROR_BAD_DEVICE = 1200&
Private Const ERROR_CONNECTION_UNAVAIL = 1201&
Private Const ERROR_EXTENDED_ERROR = 1208&
Private Const ERROR_MORE_DATA = 234
Private Const ERROR_NOT_SUPPORTED = 50&
Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Private Const ERROR_NO_NETWORK = 1222&
Private Const ERROR_NOT_CONNECTED = 2250&
Private Const NO_ERROR = 0
#End Region
#Region "API"
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias
"GetLogicalDriveStringsA" (ByVal nBufferLength As Integer, ByVal lpBuffer As
String) As Integer
Private Declare Function GetDriveType Lib "kernel32" Alias
"GetDriveTypeA" (ByVal nDrive As String) As Integer
Private Declare Function WNetGetConnection Lib "mpr.dll" Alias
"WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As
String, ByRef cbRemoteName As Integer) As Integer
#End Region
#Region "Public Method"
Public function sListAllDrives() As String 'Show Drive Letter and Phisic
Path
Dim strAllDrives As String
Dim strTmp As String
Dim Ret As String
strAllDrives = fGetDrives()
If strAllDrives <> "" Then
Do
strTmp = Mid(strAllDrives, 1,
strAllDrives.IndexOf(vbNullChar) - 1)
strAllDrives = Mid(strAllDrives,
strAllDrives.IndexOf(vbNullChar) + 1)
Select Case fDriveType(strTmp)
Case "Removable Media"
Ret &= "Removable drive : " & strTmp
Case "CD Rom"
Ret &= " CD Rom drive : " & strTmp
Case "Fixed Drive"
Ret &= " Local drive : " & strTmp
Case "Network Drive"
Ret &= " Network drive : " & strTmp
Ret &= " UNC Path : " &
fGetUNCPath(Left$(strTmp, Len(strTmp) - 1))
End Select
Loop While strAllDrives <> ""
End If
Return Ret
End Function
#End Region
#Region "Private Method"
Private Function fGetDrives() As String
'Returns all mapped drives
Dim lngRet As Integer
Dim strDrives As String = New String(Chr(32), 255)
Dim lngTmp As Integer
lngTmp = strDrives.Length
lngRet = GetLogicalDriveStrings(lngTmp, strDrives)
fGetDrives = Left(strDrives, lngRet)
End Function
Private Function fGetUNCPath(ByVal strDriveLetter As String) As String
Try
Dim Msg As String
Dim lngReturn As Integer
Dim lpszLocalName As String
Dim lpszRemoteName As String
Dim cbRemoteName As Integer
lpszLocalName = strDriveLetter
lpszRemoteName = New String(Chr(32), 255)
cbRemoteName = lpszRemoteName.Length
lngReturn = WNetGetConnection(lpszLocalName, lpszRemoteName,
cbRemoteName)
Select Case lngReturn
Case ERROR_BAD_DEVICE
Msg = New String("Error: Bad Device")
Case ERROR_CONNECTION_UNAVAIL
Msg = New String("Error: Connection Un-Available")
Case ERROR_EXTENDED_ERROR
Msg = New String("Error: Extended Error")
Case ERROR_MORE_DATA
Msg = New String("Error: More Data")
Case ERROR_NOT_SUPPORTED
Msg = New String("Error: Feature not Supported")
Case ERROR_NO_NET_OR_BAD_PATH
Msg = New String("Error: No Network Available or Bad
Path")
Case ERROR_NO_NETWORK
Msg = New String("Error: No Network Available")
Case ERROR_NOT_CONNECTED
Msg = New String("Error: Not Connected")
Case NO_ERROR
' all is successful...
End Select
If Not (Msg Is Nothing) Then
MessageBox.Show(Msg, String.Empty, MessageBoxButtons.OK,
MessageBoxIcon.Information)
Return New String(String.Empty)
Else
fGetUNCPath = Left$(lpszRemoteName,
lpszRemoteName.IndexOf(Chr(0)))
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message, String.Empty, MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Function
Private Function fDriveType(ByVal strDriveName As String) As String
Dim lngRet As Integer
Dim strDrive As String = New String(String.Empty)
lngRet = GetDriveType(strDriveName)
Select Case lngRet
Case DRIVE_UNKNOWN 'The drive type cannot be determined.
strDrive = New String("Unknown Drive Type")
Case DRIVE_ABSENT 'The root directory does not exist.
strDrive = New String("Drive does not exist")
Case DRIVE_REMOVABLE 'The drive can be removed from the drive.
strDrive = New String("Removable Media")
Case DRIVE_FIXED 'The disk cannot be removed from the drive.
strDrive = New String("Fixed Drive")
Case DRIVE_REMOTE 'The drive is a remote (network) drive.
strDrive = New String("Network Drive")
Case DRIVE_CDROM 'The drive is a CD-ROM drive.
strDrive = New String("CD Rom")
Case DRIVE_RAMDISK 'The drive is a RAM disk.
strDrive = New String("Ram Disk")
End Select
Return strDrive
End Function
#End Region
End Class
Cheers
Robert [VPDGOGYD]
Paul G. Tobey said:
That is, if you set up the share manually before the first run of your
application, entering the password, etc. (or having the user do it), if
necessary, then you'd just assume that there is a folder \Network\sharename
on computername, and go from there. If that won't work for you, you'll have
to P/Invoke to the WNET routines (WNetAddConnection, etc.). There may have
been some traffic on doing this before, so check the newsgroup archives.
Paul T.
to
use look
for into
the
protocol
should
http://groups.google.com/groups?hl=...soft.public.dotnet.framework.compactframework
directory
on System.IO.Filestream
by