-
May 27th, 2010, 04:22 AM
#1
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:
'Create a list that will hold our permissions
Dim PermissionsList As New List(Of SharePermissionEntry)
'Create a new permission entry for the Everyone group and specify that we want to allow them Read access
Dim PermEveryone As New SharePermissionEntry(String.Empty, "Everyone", SharedFolder.SharePermissions.Read, True)
'Create a new permission entry for the currently logged on user and specify that we want to allow them Full Control
Dim PermUser As New SharePermissionEntry(Environment.UserDomainName, Environment.UserName, SharedFolder.SharePermissions.FullControl, True)
'Add the two entries declared above to our list
PermissionsList.Add(PermUser)
PermissionsList.Add(PermEveryone)
'Share the folder as "Test Share" and pass in the desired permissions list
Dim Result As SharedFolder.NET_API_STATUS = _
SharedFolder.ShareExistingFolder("Test Share", "This is a test share", "C:\SomeFolder", PermissionsList)
'Show the result
If Result = SharedFolder.NET_API_STATUS.NERR_Success Then
MessageBox.Show("Share created successfully!")
Else
MessageBox.Show("Share was not created as the following error was returned: " & Result.ToString)
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|