Creating a Shared Folder

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hi -

I understand how to create a directory folder, but how can I programatically
create a _shared_ directory folder and set its permissions?? (I'm using
VB.NET.)

Thanks for your help.

- Jeff
 
you can create a shared folder by shelling out to the shell command "net"

ie shell("net share sharename=c:\directoryname")

there are some basic options for permissions on this command. try net share
/? for details..

As for going any further than that with permissions, I'm afraid I can't
help..

regards

simon
 
Thanks, Simon -

I'm still looking for a ".NET" way to create a shared directory, and share
net doesn't appear to solve the permissions problem.

Any other ideas?? Anyone?

- Jeff
 
Hi Jeff,

I think you may try to use the WMI approach to do the job.
Here is a useful google link you may take a look.
http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&selm=3FC0F8E6.1BF1456A
%40hydro.com

In .net we can use the System.Management to manipulate the WMI.
Code: Finding the Amount of Disk Space Available (Visual Basic)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vbcode/h
tml/vbtskCodeFindingAmountOfDiskSpaceAvailableVisualBasic.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi -

This post is in response to Peter Huang's request for feedback on his
response to my question.

First, Peter, thank you for your response. But my issue remains unresolved.
The difficulty I'm having with your response may be that you perhaps
provided too much(!) information. I can't use some of it, but I'm having to
wade through all of it to find the useful bits. (My comments on the
specific links that you provided are below.)

I had thought that my initial question was straightforward: I need to
create a shared folder and set its permissions using VB.NET. It seems that,
to do so, I must learn a whole new technology (wmi). While I'm willing to
do that, it's taking some time. Wmi documentation is not all that
intuitive, and I've found a few tantalizingly-but-not-exactly relevant
examples that I haven't yet been able to get to work from VB.NET. I was
hoping for a more straightforward answer or example when I posted my
question.

My further research has alerted me to some additional issues that may be
relevant. So, in the interest of clarifying/focusing my question:

- I'm creating a commercial VB.NET application
- I need to synchronize two databases, one SQL2K database on a desktop or
network, and one SQLCE database on a pocket pc handheld
- To accomplish that synchronization, I need to use SQL's merge replication
- To implement that merge replication, I need to create a shared folder and
set its permissions
- I will not have access to users' computers, so all of this must be done by
my app
- I'm trying to hide as much complexity as possible from the user
- Users may be using a variety (I have no control over this) of Windows
versions
- Users can be single users trying to synchronize their desktop with their
PDA (I'll need to distribute and install MSDE for these users)
- Users also can be corporate users, synchronizing many PDAs with a
corporate SQL database.

Your response provided links to two web pages:

1.
http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&[email protected]
2.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vbcode/html _

/vbtskCodeFindingAmountOfDiskSpaceAvailableVisualBasic.asp

The second link is an MSDN Library page discussing how to use wmi to find
available disk space. While that code seems to work, it doesn't address my
search for a way to create a share and set permissions. Since there are
other MSDN Library articles that more closely approach those issues, I'm not
sure why you suggested I look at the finding disk space article instead of
more relevant articles.

The first link was to a forum post about setting file share permissions
using a script. That page didn't answer the question, but it provided links
to 14 other sites/pages:

a) http://www.microsoft.com/technet/scriptcenter/filefolder/scrff33.asp
b)
http://msdn.microsoft.com/library/en-us/netdir/adsi/iadssecurityutility_getsecuritydescriptor.asp
c)
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&[email protected]
d)
http://msdn.microsoft.com/library/en-us/netdir/adsi/iadssecurityutility_setsecuritydescriptor.asp
e)
http://groups.google.com/[email protected]
f) http://groups.google.com/[email protected]
g) http://www.jsiinc.com/asp/reghacks.asp?tipnumb=6353
h)
http://www.microsoft.com/windows2000/downloads/servicepacks/sp4/supporttools.asp
i) http://www.somarsoft.com/
j) http://www.microsoft.com/technet/scriptcenter/filefolder/scrff13.asp
k) http://www.windows2000faq.com/Articles/Index.cfm?ArticleID=14459
l) http://www.windows2000faq.com/Articles/Index.cfm?ArticleID=14842
m) http://setacl.sourceforge.net/
n) http://www.microsoft.com/technet/scriptcenter

While I may have misread or misunderstood some of these, my
experience/reaction to each of these links follows:

Link (a) was to a technet script center, which I'm working my way through.
Link (b) 404 Location Cannot Be Found
Link (c) was to a forum thread re Enumerating Share Permissions. While that
may be useful for other purposes, it doesn't tell me how to set those
permissions in the first place (or how to create the share).
Link (d) 404 Location Cannot Be Found
Link (e) was to a forum thread from 2001 re using wmi to add a user to a
share folder. While this might be useful to me (and answers a question that
perhaps I should have asked), it doesn't address how to implement the
approach in VB.NET.
Link (f) was to a forum post re Setting security on all shares. While
perhaps relevant, again it's not VB.NET.
Link (g) was to a page linking to a download of Microsoft's public domain
RMTShar.exe for remotely sharing a number of resources. I'm not sure I need
the remote part of this, and I need to set up the share and permissions from
within my VB.NET program.
Link (h) was to a page for Win2K Support Tools. I haven't tried these yet,
but my impression was that they don't address my need to do this from within
my VB.NET program.
Link (i) was to a product page for a free utility to dump security settings.
I don't currently need to do that. There also was a link on that page to a
product page for a (non-free) utility that may do what I need, but not from
within my VB.NET program.
Link (j) linked me to the same technet script center I found from link (a)
Link (k) was to a forum post suggesting how to use RMTShar.exe (see link
(g)) to do what I need, but from a command line. Is it really necessary (or
simpler) to shell out to a command line program to get this done??
Link (l) was to a forum post suggesting a way to create a share from the
command line, again not from a VB.NET program.
Link (m) was to a product page for SETACL, a tool to manage permissions.
While I can probably figure out how to use this from a VB.NET program, I
scanned their documentation and was unable to find a way to create a share.
(I might have missed something.)
Link (n) again linked me to the same technet script center I found from link
(a) and link (j)

- Jeff
 
Hi Jeff,

I am sorry if I have made any confusion.
Here goes the code. Please take a look at the comments in the code.
Basically we need to read the links below to achieve the job.

How to use the Win32_Share to create network share
Win32_Share
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
win32_share.asp
Create Method of the Win32_Share Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
create_method_in_class_win32_share.asp

How to set the security account
Win32_ACE
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
win32_ace.asp
Win32_Trustee
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
win32_trustee.asp

How to locate a user account by ldap path
LDAP ADsPath
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/l
dap_adspath.asp


<code>
Imports System.Management
Imports System.DirectoryServices
Module Module1
Sub Main()
Dim serverName, UserName, AdminName, UserAcct As String
Dim Ace1 As Management.ManagementObject
serverName = "TestServer"
Dim scope As New
Management.ManagementScope("\\serverName\root\cimv2")
'Win32_Share

'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi
/win32_share.asp
Dim wmiShare As New Management.ManagementClass(scope, New
Management.ManagementPath("Win32_Share"), Nothing)
'Define the LDAP path to the desired user account.
'LDAP ADsPath

'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/
ldap_adspath.asp
UserAcct = "CN=Jeff Smith,CN=users,DC=fabrikam,DC=com"
'Get the user's information from the Active Directory
Dim UserNamePath As String = "LDAP://" & UserAcct
Dim DirEnt As New DirectoryServices.DirectoryEntry(UserNamePath)
'Get the User's SID and samAccountname. It will be used when
creating the DACL
UserName = DirEnt.Properties("samaccountname")(0)
Dim UserSID As Byte() = DirEnt.Properties("objectsid")(0)
DirEnt.Dispose()

'Set an ACE for the user
Ace1 = SetAce(scope, 2032127, 3, 0, SetTrustee(scope, serverName,
UserName, UserSID))

'Place the aces into a Win32_SecurityDescriptor
Dim secDescriptor As Management.ManagementObject
secDescriptor = New Management.ManagementClass(scope, New
Management.ManagementPath("Win32_SecurityDescriptor"),
Nothing).CreateInstance()
secDescriptor("ControlFlags") = 4
secDescriptor("DACL") = New Management.ManagementObject() {Ace1}

'Prepare to invoke the CREATE method on Win32_Share by specifying
the parameters
Dim inParams As Management.ManagementBaseObject
inParams = wmiShare.GetMethodParameters("Create")
inParams("Access") = secDescriptor
inParams("Path") = "C:\TestFolder"
inParams("Name") = "TestFolder"
inParams("Type") = 0
inParams("Description") = "FolderDescriptionHere"
Dim outParams As Management.ManagementBaseObject
'Invoke the CREATE method and create the share.
'Create Method of the Win32_Share Class

'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi
/create_method_in_class_win32_share.asp
outParams = wmiShare.InvokeMethod("Create", inParams, Nothing)
End Sub

Function SetTrustee(ByVal scope As Management.ManagementScope, ByVal
domain As String, ByVal name As String, ByVal UserSid As Byte()) As
Management.ManagementObject
Dim wmiTrustee As Management.ManagementObject
'Win32_Trustee

'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi
/win32_trustee.asp
wmiTrustee = New Management.ManagementClass(scope, New
Management.ManagementPath("Win32_Trustee"), Nothing).CreateInstance()
wmiTrustee("Domain") = domain
wmiTrustee("Name") = name
wmiTrustee("SID") = UserSid
Return wmiTrustee
End Function

Function SetAce(ByVal scope As Management.ManagementScope, ByVal
accessMask As Integer, ByVal aceFlags As Integer, ByVal aceType As Integer,
ByVal wmiTrustee As Management.ManagementObject) As
Management.ManagementObject
Dim wmiAce As Management.ManagementObject
'Win32_ACE

'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi
/win32_ace.asp
wmiAce = New Management.ManagementClass(scope, New
Management.ManagementPath("Win32_ACE"), Nothing).CreateInstance()
wmiAce("AccessMask") = accessMask
wmiAce("AceFlags") = aceFlags
wmiAce("AceType") = aceType
wmiAce("Trustee") = wmiTrustee
Return wmiAce
End Function
End Module

</code>

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
For starters, the code sample is great. I was looking for the same
information myself. Now, I was wondering if you could shed a little
light on something. I need to apply permissions to a share but not
for a specific user. I need to assign "Full Access" to the
"Authenticated Users" group. I can assign permisions to a specific
user without issues, but I think "Authenticated Users" is a special
case. I was able to get it to work with "Everyone" but not
"Authenticated Users".

Can you help out?

Thanks
 
Back
Top