Click here to Skip to main content
Click here to Skip to main content

Remote Share

, 23 Jun 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kraeven
Software Developer Jacobs
Belgium Belgium
IT-Specialist/Developer/Support Engineer

Comments and Discussions

 
GeneralMy vote of 4 Pinmember Gun Gun Febrianza6-Sep-13 23:53 
GeneralThanks Pinmembersam14108329-Nov-12 1:28 
GeneralRe: Thanks PinmemberKraeven29-Nov-12 2:28 
Questionpermittions of a share PinmemberAlbertOffermans28-Jun-11 0:44 
AnswerRe: permittions of a share PinmemberKraeven28-Jun-11 0:54 
GeneralRe: permittions of a share PinmemberAlbertOffermans1-Jul-11 6:26 
GeneralMy vote of 4 Pinmemberalain_dionne29-Jun-10 3:14 
GeneralRe: My vote of 4 PinmemberKraeven29-Jun-10 21:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 23 Jun 2010
Article Copyright 2010 by Kraeven
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid