Create Network Share on any computer

Submitted on: 1/3/2015 7:15:00 PM
By: Rick Casady (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: VB.NET
Views: 6120
     Create a network share in a windows environment on a local or remote computer without using DOS commands or xcacls or xlnt. This code was based on information learned in WMI Scripting Primer: Part 3 on the MSDN and I have a similar example for vbscript and on my site at Let me know what you think and I'll stop or keep going based on you feedback.

' Purpose: Create a network share for NT users. You will
' need to have correct permissions to do this and it can be
' done to remote computers.
' We pass in the folder name, path of the folder and 
' description of the share folder.
Private Sub CreateShare(strShareName, strPath, strDescription) 
	Dim objSWbemServices as object 
	Dim objSWbemObject as object
	Dim colSWbemObject as object 
 Dim intRet as integer 
 Dim blnExists as boolean 
 Dim objSWbem as object 
 ' Next we call the standard GetObject function for 
 ' returning COM objects and pass it the connection 
 ' string for connecting to the WMI.
 objSWbemServices = GetObject("winmgmts:\\.\root\cimv2") 
 ' This same line can be executed on a remote computer 
 ' with a differnt connection string like this:
 ' objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 ' Now we enumrate the Shares on the target computer and
 ' return it to a collection
 colSWbemObject = objSWbemServices.InstancesOf("Win32_Share") 
 ' Loop through each share on the machine to see if it already exists 
 	For each objSWbem in colSWbemObject 
 		If( = strShareName)Then 
			blnShareExists = True 
			Exit For 
			blnShareExists = False 
	 End If 
	' if the share didnít exist our Boolean will be false
	' and we can try to add it.
	If (blnShareExists = False)Then 
 	' Create the share 
 	' Now we need to get 
 		objSWbemObject = objSWbemServices.Get("Win32_Share") 
 		' Last we call the create passing our path, name,
 		' description and 10 is for max number of users 
 	intRet = objSWbemObject.Create(strPath, strShareName, , 10, strDescription) 
 	msgbox("Folder aready shared") 
 End If 
End Sub

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.