How to share a folder in VB.net

Apostolos Doudakmanis 121 Reputation points
2023-10-26T07:37:48.2866667+00:00

Hello

I have created a folder and I want to make it shared

I found these code from internet but it gives me a problem that it is unable to share directory

Where is the mistake?

Sub CreateShareFolder1(folderPath As String)
        Try
            Dim managementClass As New ManagementClass("Win32_Share")
            Dim inParams As ManagementBaseObject = managementClass.GetMethodParameters("Create")
            inParams("Description") = "My Description"
            inParams("Name") = "Share Name"
            inParams("Path") = folderPath
            inParams("Type") = 0
            Dim outParams As ManagementBaseObject = managementClass.InvokeMethod("Create", inParams, Nothing)
            If Convert.ToUInt32(outParams.Properties("ReturnValue").Value) <> 0 Then
                MessageBox.Show("Unable to share directory.")
            Else
                MessageBox.Show("Shared folder successfully!")
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Developer technologies VB
{count} votes

3 answers

Sort by: Most helpful
  1. Apostolos Doudakmanis 121 Reputation points
    2023-10-26T10:43:39.94+00:00

    No, I don't start it with admin rights but I have a user account with admin rights and password

    but it still doesn't give me the right to make my folder

    the only thing I can do with other codes is to add users to the folder

    NoSharedFolder1

    NoSharedFolder2

    0 comments No comments

  2. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-10-26T11:05:22.3966667+00:00

    Hi @Apostolos Doudakmanis ,

    Did you use an administrator username and password to construct the ManagementScope object and ManagementClass object?

    Please check the return value to determine the error type.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Castorix31 90,521 Reputation points
    2023-10-26T14:12:47.2966667+00:00

    This test works for me with NetShareAdd, a local folder named "E:\Test" and a Manifest file with "requireAdministrator"

                Dim si As SHARE_INFO_502 = New SHARE_INFO_502()
                si.shi502_netname = "Test Share"
                si.shi502_path = "E:\Test"
                si.shi502_remark = "Comment"
                si.shi502_type = STYPE_DISKTREE
                si.shi502_max_uses = SHI_USES_UNLIMITED
                Dim pSI As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(SHARE_INFO_502)))
                Marshal.StructureToPtr(si, pSI, False)
                Dim nIndex As UInteger = 0
                Dim nRet = NetShareAdd(Nothing, 502, pSI, nIndex)
                Marshal.FreeHGlobal(pSI)
    

    With :

       <DllImport("Netapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
       Public Shared Function NetShareAdd(servername As String, level As UInteger, buf As IntPtr, ByRef parm_err As UInteger) As UInteger
       End Function
    
       <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
       Public Structure SHARE_INFO_502
           Public shi502_netname As String
           Public shi502_type As UInteger
           Public shi502_remark As String
           Public shi502_permissions As UInteger
           Public shi502_max_uses As Integer
           Public shi502_current_uses As Integer
           Public shi502_path As String
           Public shi502_passwd As String
           Public shi502_reserved As UInteger
           Public shi502_security_descriptor As IntPtr
       End Structure
    
       Public Const STYPE_DISKTREE = 0
       Public Const STYPE_PRINTQ = 1
       Public Const STYPE_DEVICE = 2
       Public Const STYPE_IPC = 3
       Public Const STYPE_MASK = &HFF             ' And With shi_type To
       Public Const STYPE_RESERVED1 = &H1000000              ' Reserved For internal processing
       Public Const STYPE_RESERVED2 = &H2000000
       Public Const STYPE_RESERVED3 = &H4000000
       Public Const STYPE_RESERVED4 = &H8000000
       Public Const STYPE_RESERVED5 = &H100000
       Public Const STYPE_RESERVED_ALL = &H3FFFFF00
       Public Const STYPE_TEMPORARY = &H40000000
       Public Const STYPE_SPECIAL = &H80000000
    
       Public Const SHI_USES_UNLIMITED = -1
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.