Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Checking if network share exists

37 views
Skip to first unread message

John-Rock

unread,
Apr 30, 2009, 12:09:35 PM4/30/09
to
Hi,

I'm writing a console app to take care of our automated user creation
process which was previously done by a batch file. I need to check if a
users network share exist on any of 38 servers but seem to be having trouble
finding info on how to do it. From what i can tell i need to use the
netapi32.dll and the NetShareCheck function, but i'm not sure how to do it.

this is what i have so far, but it isn't working as it should. it seems to
be returning 2311 in almost all cases exept if i use uppercase on the server
and device and the device starts with C.

Imports System.Runtime.InteropServices
Public Class NetApiWin32
Declare Unicode Function NetShareCheck Lib "netapi32.dll"
(<MarshalAs(UnmanagedType.LPWStr)> ByVal ServerName As String,
<MarshalAs(UnmanagedType.LPWStr)> ByVal Device As String, ByRef Type As
Long) As Integer
End Class
#Region "SHARE_TYPE Enumeration Definition"
' <summary>Type of share</summary>
Public Enum ShareType
' <summary>Disk Share</summary>
STYPE_DISKTREE = 0
' <summary>Printer Share</summary>
STYPE_PRINTQ = 1
' <summary>Device Share</summary>
STYPE_DEVICE = 2
' <summary>IPC Share</summary>
STYPE_IPC = 3
' <summary>Special Share</summary>
' <remarks>
' Add this value to one of the above values
' to get an Administrative Share of that type
' </remarks>
STYPE_SPECIAL = &H80000000
STYPE_TEMPORARY = &H40000000

End Enum
#End Region
Private Function IsResourceShared(ByVal strServer As String, ByVal
strResourceName As String, ByRef nType As ShareType) As Boolean
Dim bRet As Boolean = False
nType = ShareType.STYPE_DISKTREE
Dim lType As Long = 0
Dim nRet As Integer = NetApiWin32.NetShareCheck(strServer,
strResourceName, lType)
bRet = (0 = nRet) ' check to see if 0 is returned
If Not bRet Then
If nRet = 2311 Then
Console.WriteLine("Device not shared")
ElseIf nRet = 2310 Then
Console.WriteLine("The device does not exist")
Else
Console.WriteLine("Unknown win32 error")
End If
Else
nType = CType(lType, ShareType)
End If
Return bRet
End Function
I got most of the code from http://www.netomatix.com/CheckShare.aspx and
converted it to vb.net. Any tips would be great. i'm having trouble finding
the info i need to get this working.

thanks

0 new messages