Sharing in VbNET

From HashVB
Jump to: navigation, search

This code is for sharing folders or files in using the System.Management references. I transcode it from C in:

This link can be usefull too:

First of all, u need add the System.Management reference. This must be definied as public in the Generals Declarations of the app.

'After, the public function called QshareFolder. 'This example is for folders, but u can change the values for files. 'Read the ManagementBaseObject MSDN. Also u can add more inParameters values.


'Importing the reference 
Imports System.Management

Public Sub QshareFolder(ByVal FolderPath As String, ByVal ShareName As String, ByVal Description As String)
            'Create a ManagementClass object

            Dim ManagementClass As New ManagementClass("Win32_Share")
            '   Create ManagementBaseObjects for in and out parameters

            Dim inParams As ManagementBaseObject = ManagementClass.GetMethodParameters("Create")
            Dim outParams As ManagementBaseObject
            ' Set the input parameters

            inParams("Description") = Description
            inParams("Name") = ShareName

            inParams("Path") = FolderPath

            inParams("Type") = 0 'Disk Drive

            outParams = ManagementClass.InvokeMethod("Create", inParams, Nothing)

            If outParams.Properties("ReturnValue").Value <> 0 Then
              'error if u cant do it
            End If

        Catch ex As Exception
            'error if u cant do it
        End Try
    End Sub

This work perfect for me, in VB.NET 2005 NF2 SP2 (XP)

Its my first contribution, so sorry for my poor english :)

Nocturno - Nocturno27