VB.NET – Sharing a folder

May 25, 2010 — 1 Comment

Here is an example of how you can use the NetShareAdd Windows API to share an existing folder either on your local machine or on a remote machine. Unfortunately there is currently no managed .NET method to do this so Windows API or WMI are the only ways you can do this… and I don’t like WMI for several reasons so I chose the API route.

Here are the API definitions (remember to import System.Runtime.InteropServices at the start of your code where you define these) :

‘Used to specify that we want to share a folder
Public Const STYPE_DISKTREE As UInteger = 0

”’ <summary>
”’ Values returned by the NetShareAdd function
”’ </summary>
Public Enum NET_API_STATUS As Integer
End Enum

”’ <summary>
”’ Used by the NetShareAdd API to specify basic share settings
”’ </summary>
<StructLayoutAttribute(LayoutKind.Sequential)> _
Public Structure SHARE_INFO_2
”’ <summary>
”’ The share name
”’ </summary>
<MarshalAsAttribute(UnmanagedType.LPWStr)> _
Public shi2_netname As String
”’ <summary>
”’ The share type – specify STYPE_DISKTREE for a folder
”’ </summary>
Public shi2_type As UInteger
”’ <summary>
”’ A description or comment for the share
”’ </summary>
<MarshalAsAttribute(UnmanagedType.LPWStr)> _
Public shi2_remark As String
”’ <summary>
”’ Share-level permissions – not supported by Windows so set to 0
”’ </summary>
Public shi2_permissions As UInteger
”’ <summary>
”’ The maximum number of connections to this share that should be allowed
”’ </summary>
Public shi2_max_uses As Integer
”’ <summary>
”’ Ignored when used with NetShareAdd, so set to 0
”’ </summary>
Public shi2_current_uses As UInteger
”’ <summary>
”’ The local path to the folder that is to be shared
”’ </summary>
<MarshalAsAttribute(UnmanagedType.LPWStr)> _
Public shi2_path As String
”’ <summary>
”’ Share-level password – not supported by Windows so set to Nothing
”’ </summary>
<MarshalAsAttribute(UnmanagedType.LPWStr)> _
Public shi2_passwd As String
End Structure

”’ <summary>
”’ Shares a specified folder. Returns NET_API_STATUS.NERR_SUCCESS if successful
”’ </summary>
”’ <param name=”servername”>The computer on which to create the share</param>
”’ <param name=”level”>The level of information that will be passed in via the buf argument, set to 2 for basic share creation</param>
”’ <param name=”buf”>An instance of the SHARE_INFO_2 structure which has been populated with the desired share settings</param>
”’ <param name=”parm_err”>If an error is found with one of the previous arguments then the number of the argument will be returned in this value</param>
<DllImportAttribute(“netapi32.dll”, EntryPoint:=“NetShareAdd”)> _
Public Shared Function NetShareAdd(<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal servername As String, ByVal level As UInteger, <InAttribute()> ByRef buf As SHARE_INFO_2, <OutAttribute()> ByRef parm_err As UInteger) As NET_API_STATUS
End Function

and here is an example function that you might use to wrap this API’s functionality up into to use in your program:

”’ <summary>
”’ Shares a local folder. Returns NET_API_STATUS.NERR_SUCCESS if successful
”’ </summary>
”’ <param name=”ComputerName”>The computer to create the share on, pass Nothing to use the local computer</param>
”’ <param name=”LocalPath”>The full path to the folder that is to be shared</param>
”’ <param name=”ShareName”>The name that the share should have</param>
”’ <param name=”ShareDescription”>An optional description of the share</param>
Public Shared Function ShareFolder(ByVal ComputerName As String, ByVal LocalPath As String, ByVal ShareName As String, ByVal ShareDescription As String) As NET_API_STATUS
‘Create an instance of SHARE_INFO_2 and populate it with the specified settings
Dim ShareInfo As New SHARE_INFO_2
With ShareInfo
.shi2_netname = ShareName
.shi2_type = STYPE_DISKTREE
.shi2_remark = ShareDescription
.shi2_permissions = 0
.shi2_max_uses = -1
.shi2_current_uses = 0
.shi2_path = LocalPath
.shi2_passwd = Nothing
End With
‘Used as an output parameter
Dim ParameterError As UInteger = 0
‘Call the NetShareAdd API
Dim Result As NET_API_STATUS = NetShareAdd(ComputerName, 2, ShareInfo, ParameterError)
Return Result
End Function

I am also currently working on using that same NetShareAdd API to set Share permissions when the shared folder is created but this is proving to be extremely tricky – when you want to set share permissions (not NTFS permissions) it gets a lot more complicated than the code posted in this blog post and is turning into the most complex Windows API I have tried to use. I cannot find any examples on the internet of .NET code calling this function to set Share permissions (a couple of C++ examples but none of them are easy to convert) but I am determined to get it working and if I do I will post the final working code here.

EDIT: I finally got it working so here is my post that shows how to set the permissions as well: https://cjwdev.wordpress.com/2010/05/27/shared-a-folder-and-setting-share-permissions-from-vb-net/

Trackbacks and Pingbacks:

  1. Shared a folder and setting share permissions from VB.NET « Cjwdev - May 27, 2010

    […] a folder and setting share permissions from VB.NET As mentioned in my previous post I have been working on figuring out how to set share permissions (not NTFS permissions) from a […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s