Wrox Programmer Forums
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 17th, 2003, 04:03 AM
Authorized User
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sharing a Folder

Hello all,

I am having a problem trying to share a folder using the following code;

Set cont = GetObject("C:\folder")
objShare = "folder$"
Set fs = cont.Create("FileShare", objShare)

I am getting the following error on the first line;

"File name or class name not found during Automation operation"
Any help or suggestions would be greatly Appreciated

Old December 17th, 2003, 10:18 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi

hi there..

where do you find it??

i dont think that folders can be object that you can get.. did you see it somewhere??

maybe you are looking for filesystemobject???


Gonzalo Bianchi
Old December 18th, 2003, 02:25 AM
Authorized User
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts


I got the code from another forum off this site, it was for a network share though and I thought that I might be able to adapt it to share a folder.

I think that you are right about the folder not being an object that you can get, just after you posted that I found some other code that worked perfectly.

Thanks for you help though,

Old December 18th, 2003, 10:08 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts

Well in that case would you mind posting the code?


Larry Asher
Old December 19th, 2003, 05:45 AM
Authorized User
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts

OK, create a form with 5 text boxes and one command button (labels optional)

Text box 1 - Machine
Text box 2 - Folder
Text box 3 - Share Name
Text box 4 - Comment
Text box 5 - Password

Post the following code behind the form and off you go...


Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Copyright ©1996-2003 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
Private Const NERR_SUCCESS As Long = 0&

'share types
Private Const STYPE_ALL As Long = -1 'note: my const
Private Const STYPE_DISKTREE As Long = 0
Private Const STYPE_PRINTQ As Long = 1
Private Const STYPE_DEVICE As Long = 2
Private Const STYPE_IPC As Long = 3
Private Const STYPE_SPECIAL As Long = &H80000000

Private Const ACCESS_READ As Long = &H1
Private Const ACCESS_WRITE As Long = &H2
Private Const ACCESS_CREATE As Long = &H4
Private Const ACCESS_EXEC As Long = &H8
Private Const ACCESS_DELETE As Long = &H10
Private Const ACCESS_ATRIB As Long = &H20
Private Const ACCESS_PERM As Long = &H40
Private Const ACCESS_ALL As Long = ACCESS_READ Or _
                                        ACCESS_WRITE Or _
                                        ACCESS_CREATE Or _
                                        ACCESS_EXEC Or _
                                        ACCESS_DELETE Or _
                                        ACCESS_ATRIB Or _

Private Type SHARE_INFO_2
  shi2_netname As Long
  shi2_type As Long
  shi2_remark As Long
  shi2_permissions As Long
  shi2_max_uses As Long
  shi2_current_uses As Long
  shi2_path As Long
  shi2_passwd As Long
End Type

Private Declare Function NetShareAdd Lib "netapi32" _
  (ByVal servername As Long, _
   ByVal level As Long, _
   buf As Any, _
   parmerr As Long) As Long

Private Sub Form_Load()

   Text1.Text = "\\" & Environ$("COMPUTERNAME")
   Text2.Text = "c:\program files\adobe"
   Text3.Text = "vbnetdemo"
   Text4.Text = "VBnet demo test share"
   Text5.Text = ""

End Sub

Private Sub Command1_Click()

   Dim success As Long

   success = ShareAdd(Text1.Text, _
                      Text2.Text, _
                      Text3.Text, _
                      Text4.Text, _

   Select Case success
      Case 0: MsgBox "share created successfully!"
      Case 2118: MsgBox "share name already exists"
      Case Else: MsgBox "create error " & success
   End Select

End Sub

Private Function ShareAdd(sServer As String, _
                          sSharePath As String, _
                          sShareName As String, _
                          sShareRemark As String, _
                          sSharePw As String) As Long

   Dim dwServer As Long
   Dim dwNetname As Long
   Dim dwPath As Long
   Dim dwRemark As Long
   Dim dwPw As Long
   Dim parmerr As Long
   Dim si2 As SHARE_INFO_2

  'obtain pointers to the server, share and path
   dwServer = StrPtr(sServer)
   dwNetname = StrPtr(sShareName)
   dwPath = StrPtr(sSharePath)

  'if the remark or password specified,
  'obtain pointer to those as well
   If Len(sShareRemark) > 0 Then
      dwRemark = StrPtr(sShareRemark)
   End If

   If Len(sSharePw) > 0 Then
      dwPw = StrPtr(sSharePw)
   End If

  'prepare the SHARE_INFO_2 structure
   With si2
      .shi2_netname = dwNetname
      .shi2_path = dwPath
      .shi2_remark = dwRemark
      .shi2_type = STYPE_DISKTREE
      .shi2_permissions = ACCESS_ALL
      .shi2_max_uses = -1
      .shi2_passwd = dwPw
   End With

  'add the share
   ShareAdd = NetShareAdd(dwServer, _
                          2, _
                          si2, _

End Function
'--end block--'

Similar Threads
Thread Thread Starter Forum Replies Last Post
App_Code folder shared on only 1 folder? rpeters83 ASP.NET 2.0 Professional 1 September 1st, 2006 10:53 PM
Sharing code GuyB CSS Cascading Style Sheets 1 July 6th, 2006 09:41 PM
Sharing code GuyB ASP.NET 2.0 Basics 2 July 6th, 2006 06:50 PM
Making a folder virtual folder on button click in CsharpHelp C# 0 October 26th, 2005 05:57 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.