Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Remote Share

0.00/5 (No votes)
23 Jun 2010 1  
Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares.

Introduction

Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been included in the article.

Background

This tool has been made to facilitate creating shares on remote machines. Many times, it has been very handy, a user wants some files on this machine. You simply create a share on this machine, browse to the users' desktop and place the files right under their nose...

Using the Code

The code is pretty straight forward in using, it exists in 2 versions. A GUI version and a Console version.

The main functions are self-explanatory, one for creating a share, one for removing a share and of course a function that gives you a list of the available shares on that machine. You must be an administrator on the remote computer for the connection to be made through WMI.

For the moment, no impersonation is implemented.

Screenshots

Remote Share GUI

RemoteShare2.jpg

Remote Share Console

RemoteShare3.jpg

Main Functions

All of the functions are based on WMI methods, these methods will do the dirty work for you.

What's WMI? Windows Management Instrumentation

Using the .NET Framework namespace System.Management, you can develop applications that obtain enterprise data and automate administrative tasks using Windows Management Instrumentation (WMI). You can also develop applications that provide data to WMI classes using the WMI Provider Extensions.

For more information about WMI, please visit the MSDN website.

Create Share

This function will allow you to create a share on a local or remote machine using WMI. To execute the function below, you will have to specify the name of the share, the path, the computer name and if you like, a bit of information about the share.

The most important method in this function is of course the WMI Create Share Method. WMI Create Share Function is constructed like this:

WMIObject.Create_
(Path,Name,Type,MaximumAllowed,Description,Password,Win32_SecurityDescriptor Access)  

Create Method of the Win32_Share Class

  Public Sub CreateShare(ByVal strShareName As String, ByVal strPath As String,
        ByVal computername As String, ByVal shareinfo As String)
        Try
            Dim objSWbemServices As Object
            Dim objSWbemObject As Object
            Dim colSWbemObject As Object
            Dim intRet As Integer
            Dim blnExists As Boolean
            Dim objSWbem As Object

            objSWbemServices = GetObject("winmgmts://" + computername + "/root/cimv2")

            colSWbemObject = objSWbemServices.InstancesOf("Win32_Share")

            For Each objSWbem In colSWbemObject
                If (objSWbem.name = strShareName) Then
                    blnExists = True
                    Exit For
                Else
                    blnExists = False
                End If
            Next

            If (blnExists = False) Then
                objSWbemObject = objSWbemServices.Get("Win32_Share")
                intRet = objSWbemObject.Create(strPath, strShareName, 0, 25, shareinfo)
            Else
                MsgBox("Folder already shared")
            End If
        Catch ex As Exception
            MsgBox("Error occurred while trying to create shares on remote pc." + 
                vbCrLf + "Check if you have the necessary rights and/or that" +
                "the pc is turned on." + vbCrLf + ex.ToString)
        End Try

    End Sub 

Remove Share

This function will allow you to remove shares from the machine either local or remote. Obviously the function needs 2 parameters, the name of the share and the computer name. The WMI Query (see snippet below) will collect all the shares with the name provided. Afterwards, we'll loop through these objects and simply delete them.

The method that does the deletion of the share is very simple and looks like this:

WMIObject.Delete()  

Delete Method of the Win32_Share Class

Public Function RemoveShare(ByVal shareName As String,
    ByVal computername As String) As Boolean
        Try
            Dim objSWbemServices As Object
            Dim objSWbemObject As Object
            Dim colSWbemObject As Object

            objSWbemServices = GetObject(
                "winmgmts:{impersonationLevel=impersonate}!\\" & 
                computername & "\root\cimv2")
            colSWbemObject = objSWbemServices.ExecQuery(
                "SELECT * FROM Win32_Share WHERE Name = '" + shareName + "'")
            For Each objSWbemObject In colSWbemObject
                objSWbemObject.Delete()
            Next
        Catch ex As Exception
            MsgBox("Error occurred while trying to remove shares on remote pc." + 
            vbCrLf + "Check if you have the necessary rights and/or that the pc" +
            "is turned on." + vbCrLf + ex.ToString)
        End Try

    End Function 

Show Shares

This function requires the computer name and will make the connection to that machine. By using a WMI query that gets all instance of Win32_Share will fill the ListView on the form (if you use the GUI version):

 Public Function ShowShares(ByVal ComputerName as String) As Boolean
        Try
            ListView1.Items.Clear()
            Dim objSWbemServices As Object
            Dim colSWbemObject As Object
            objSWbemServices = GetObject("winmgmts://" + ComputerName + "/root/cimv2")
            colSWbemObject = objSWbemServices.InstancesOf("Win32_Share")
            ' Loop through each share on the machine to see if it already exists 
            For Each objSWbem In colSWbemObject
                Dim lvitem As New ListViewItem
                With lvitem
                    .Text = objSWbem.name
                    .SubItems.Add(objSWbem.path)
                    .SubItems.Add(objSWbem.description)
                End With
                ListView1.Items.Add(lvitem)
            Next
        Catch ex As Exception
            MsgBox("Error occurred while trying to show shares on remote pc." + vbCrLf + 
            "Check if you have the necessary rights and/or that the pc is turned on." + 
            vbCrLf + ex.ToString)
        End Try

    End Function

Additional Links

History

  • Initial version 23-06-2010

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here