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) :
and here is an example function that you might use to wrap this API’s functionality up into to use in your program:
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/