How to programmatically create shared folder and assign permission using .Net C# ?

How to programmatically create shared folder and assign permission using .Net C# ?

Please provide sample code.
sepknowAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
I would use WMI to create a share with the Win32_Share class:

Create method of the Win32_Share Class
http://msdn.microsoft.com/en-us/library/windows/desktop/aa389393(v=vs.85).aspx

Assigning permission is another thing.  I would have to research that one.
0
 
Mihai StancescuConnect With a Mentor Software Engineer Commented:
For sharing you can use WMI as TheLearnedOne said:
WMI share example or you can use pinovke PInvoke share.

To apply permissions: Folder permissions.
To set sharing permissions you can use WMI again: WMI Share permissions.

Regards,
Mishu
0
 
sepknowAuthor Commented:
I am not familiar with unmanaged code.
I am not able to compile this statement :

using ActiveDs;

May I know which reference should be added?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Bob LearnedCommented:
WMI is managed code that is in the System.Management namespace.  You need to add a reference to your project to the System.Management.dll, since it is not added by default.
0
 
sepknowAuthor Commented:
I think I am close to what I need. Just this one piece missing.

This statement gave everybody full control:
shInfo.shi502_security_descriptor = IntPtr.Zero;

How to set this structure such that the "Administrator" has Read/Write and the rest has only Read access?
0
 
Bob LearnedCommented:
I am not sure what you are working with there.  Can you expound on that idea, please?
0
 
sepknowAuthor Commented:
Hi,

Thanks.

I have 3 local user accounts in my computer.
1 built-in administrator and two other under the Users group.

What I need is to be able to (1) create a shared folder and for this newly created shared folder, (2a) assign the read and write permission to the administrator, (2b) assign read access to two other user account, finally (3) remove the Everyone full control permission (cause when the shared folder was created, Everyone full control permission was assigned automatically).
0
 
Bob LearnedCommented:
Can you provide more detail about this concept?

shInfo.shi502_security_descriptor = IntPtr.Zero;

What type of structure is that?  Is that WMI?  I don't recognize the names...
0
 
sepknowAuthor Commented:
Sorry for late reply.
I have found an alternate solution.
Thank you for your effort.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.