Click here to Skip to main content
15,886,258 members
Articles / Desktop Programming / Windows Forms

Remote Share

Rate me:
Please Sign up or sign in to vote.
4.43/5 (6 votes)
23 Jun 2010CPOL2 min read 63.7K   6.7K   23   8
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:

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

Create Method of the Win32_Share Class

VB.NET
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:

VB.NET
WMIObject.Delete()  

Delete Method of the Win32_Share Class

VB.NET
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):

VB.NET
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)


Written By
Software Developer Jacobs
Belgium Belgium
IT-Specialist/Developer/Support Engineer

Comments and Discussions

 
GeneralMy vote of 4 Pin
Gun Gun Febrianza6-Sep-13 22:53
Gun Gun Febrianza6-Sep-13 22:53 
GeneralThanks Pin
sam14108329-Nov-12 0:28
sam14108329-Nov-12 0:28 
GeneralRe: Thanks Pin
Kraeven29-Nov-12 1:28
Kraeven29-Nov-12 1:28 
Questionpermittions of a share Pin
AlbertONL27-Jun-11 23:44
AlbertONL27-Jun-11 23:44 
AnswerRe: permittions of a share Pin
Kraeven27-Jun-11 23:54
Kraeven27-Jun-11 23:54 
GeneralRe: permittions of a share Pin
AlbertONL1-Jul-11 5:26
AlbertONL1-Jul-11 5:26 
GeneralMy vote of 4 Pin
alain_dionne29-Jun-10 2:14
professionalalain_dionne29-Jun-10 2:14 
GeneralRe: My vote of 4 Pin
Kraeven29-Jun-10 20:30
Kraeven29-Jun-10 20:30 

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

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