Results 1 to 14 of 14

Thread: Creating shared folders and specifying share permissions

Threaded View

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Creating shared folders and specifying share permissions

    OK so I could not find a single example anywhere on the web for setting Share permissions (not NTFS permissions) from .NET code so I decided to post my own code here now that I have got it all working. There are plenty of examples for just creating shares and not specifying any permissions, and examples of how to set the permissions in C++ but not any working implementations of the relevant APIs in .NET (not that I could find anyway). Yes you can do it via WMI but personally I avoid using WMI whenever there is an alternative method so I've spent the last 2 days trying to get the various Windows APIs required to do this working from a VB.NET program... and I've finally finished it

    Rather than just post the API definitions and leave it for you to figure out exactly how to use them to get the desired effect (ie to add multiple permissions, or deny permissions etc), I've created a method that you can call which will do all of the API work for you, so you can simply call this method and pass in the share name, comment, local path to the folder that is to be shared, and a list of users with the share permissions you want each of them to have

    I will post the definitions in the next post but here is an example of how you can use it once you have got them:

    vb Code:
    1. 'Create a list that will hold our permissions
    2.         Dim PermissionsList As New List(Of SharePermissionEntry)
    3.  
    4.         'Create a new permission entry for the Everyone group and specify that we want to allow them Read access
    5.         Dim PermEveryone As New SharePermissionEntry(String.Empty, "Everyone", SharedFolder.SharePermissions.Read, True)
    6.         'Create a new permission entry for the currently logged on user and specify that we want to allow them Full Control
    7.         Dim PermUser As New SharePermissionEntry(Environment.UserDomainName, Environment.UserName, SharedFolder.SharePermissions.FullControl, True)
    8.  
    9.         'Add the two entries declared above to our list
    10.         PermissionsList.Add(PermUser)
    11.         PermissionsList.Add(PermEveryone)
    12.  
    13.         'Share the folder as "Test Share" and pass in the desired permissions list
    14.         Dim Result As SharedFolder.NET_API_STATUS = _
    15.         SharedFolder.ShareExistingFolder("Test Share", "This is a test share", "C:\SomeFolder", PermissionsList)
    16.  
    17.         'Show the result
    18.         If Result = SharedFolder.NET_API_STATUS.NERR_Success Then
    19.             MessageBox.Show("Share created successfully!")
    20.         Else
    21.             MessageBox.Show("Share was not created as the following error was returned: " & Result.ToString)
    22.         End If

    So this example shares the folder C:\SomeFolder as "Test Share" and grants the Everyone group Read access and the currently logged on user Full Control.
    Note that this does not set NTFS permissions - just Share permissions. If you want to set the NTFS permissions as well then look here: http://www.vbforums.com/showthread.php?p=3808272

    Also note that these APIs do not seem to work on a 64 bit OS if your program is set to target 64 bit CPU - if you set your project to target x86 then it works fine on 64 bit and 32 bit OS but if you set to AnyCPU then it will only work correctly on a 32 bit OS.
    Last edited by chris128; May 27th, 2010 at 07:11 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width