As mentioned in my previous post I have been working on figuring out how to set share permissions (not NTFS permissions) from a VB.NET app for the last couple of days… and I have finally got it working. This code makes use of the NetShareAdd Windows API along with several others to create the share and the ACL to go with it that specifies the permissions. Note that this can also be run against remote computers to create shares on them as well.
I could not find a single working example of doing this from .NET code anywhere on the internet (without using WMI) so hopefully this will help quite a few people out that have spent ages searching, like I did, for an example of the API definitions. I’ve also included a .NET method that wraps up some of the functionality provided by the APIs to make it easier for people to use – so that rather than looking through 150 lines of API stuff to find the bit that you need to change to set the specific permissions you want, you can just call my SharedFolder.ShareExistingFolder method and pass in the relevant parameters and it will do all of the API work for you.
EDIT: Rather than having all of the code in this blog post and on vbforums, I decided it would be easier to just have one location so that I dont have to keep both up to date, so here is my vbforums post where I posted my classes and explanation/examples: http://www.vbforums.com/showthread.php?t=616267
The code uses the following API functions and structures: NetShareAdd – SHARE_INFO_502 – SECURITY_DESCRIPTOR – EXPLICIT_ACCESS – TRUSTEE – InitializeSecurityDescriptor – SetEntriesInAcl – SetSecurityDescriptorDacl – IsValidSecurityDescriptor
Please let me know if you found this useful or have any questions/suggestions 🙂