I've been looking for a sample for getting shares from a remote computer for days. And tried many.
The following is the latest.
I used it like this:
Tried different strings for "Server"
Like \\LAPTOP\Users" and \\LAPTOP\ and LAPTOP ...
None work.
LAPTOP being one of the remote nodes on my network.
Can you help?
The following is the latest.
I used it like this:
VB.NET:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ServerShareCollection = GetSharesFromHost("Server")
If ServerShareCollection.Shares IsNot Nothing Then
For Each s As ShareType In ServerShareCollection.Shares
MsgBox(s.Name)
Next
End If
End Sub
Like \\LAPTOP\Users" and \\LAPTOP\ and LAPTOP ...
None work.
LAPTOP being one of the remote nodes on my network.
Can you help?
VB.NET:
Public Function GetSharesFromHost(ByVal server As String) As ShareCollection
Dim Shares As New ShareCollection
Shares.Clear()
Dim level As Integer = 2
Dim svr As New StringBuilder(server)
Dim entriesRead As Integer, totalEntries As Integer, nRet As Integer, hResume As Integer = 0
Dim pBuffer As IntPtr = IntPtr.Zero
Try
nRet = NetShareEnum(svr, level, pBuffer, -1, entriesRead, totalEntries, hResume)
If ERROR_ACCESS_DENIED = nRet Then
'Need admin for level 2, drop to level 1
level = 1
nRet = NetShareEnum(svr, level, pBuffer, -1, entriesRead, totalEntries, hResume)
End If
If 0 = nRet AndAlso entriesRead > 0 Then
Dim t As Type = IIf((2 = level), GetType(SHARE_INFO_2), GetType(SHARE_INFO_1))
Dim offset As Integer = Marshal.SizeOf(t)
Dim i As Integer = 0, lpItem As Integer = pBuffer.ToInt32()
While i < entriesRead
Dim pItem As New IntPtr(lpItem)
If 1 = level Then
Dim si As SHARE_INFO_1 = DirectCast(Marshal.PtrToStructure(pItem, t), SHARE_INFO_1)
Shares.Add(si.netname, "Access Denied", si.ShareType, si.Remark)
Else
Dim si As SHARE_INFO_2 = DirectCast(Marshal.PtrToStructure(pItem, t), SHARE_INFO_2)
Shares.Add(si.shi2_netname, si.shi2_path, si.shi2_type, si.shi2_remark)
End If
i += 1
lpItem += offset
End While
End If
Return Shares
Finally
' Clean up buffer allocated by system
If IntPtr.Zero <> pBuffer Then
NetApiBufferFree(pBuffer)
End If
End Try
Return Shares
End Function
Last edited by a moderator: