Remote Share

Published:
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.

Usage

Enter a computername, Share Name, Share Path and Share Description if needed

You can get a list of shares by pressing the Show button
You can Create a share if the necessary information has been filled in by pressing Create
You can Remove a share by selecting the share and pressing Remove
You can Open the share by dubbleclicking on the share listed in the listbox

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 create 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 an administrator on the remote computer for the connection to be made through WMI.

For the moment no impersonation is implemented.

Screenshot

GUI
Remote Share GUI
Remote Share In Actions
Console
Remote Share Console



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)
                                      System.Console.WriteLine("Share has been created on : " + computername)
                                      System.Console.WriteLine("ShareName : " + ShareName + "   SharePath : " + SharePath + "   ShareInfo : " + shareinfo)
                                  Else
                                      System.Console.WriteLine("Share is already present on computer : " + computername)
                                      System.Console.WriteLine("ShareName : " + ShareName + "   SharePath : " + SharePath + "   ShareInfo : " + shareinfo)
                                  End If
                              Catch ex As Exception
                                  System.Console.Write("Error occured while trying to create shares on remote pc : " + computername + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
                                  System.Console.WriteLine("ShareName : " + ShareName + "   SharePath : " + SharePath + "   ShareInfo : " + shareinfo)
                              End Try
                      
                          End Sub
                          

Open in new window


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 Boolean
                              Try
                      
                                  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 
                                  System.Console.WriteLine("Remote Shares on : " + ComputerName)
                                  For Each objSWbem In colSWbemObject
                                      System.Console.WriteLine("ShareName : " + objSWbem.name + "   SharePath : " + objSWbem.path + "   ShareInfo : " + objSWbem.description)
                                  Next
                              Catch ex As Exception
                                  System.Console.Write("Error occured while trying to show shares on remote pc : " + ComputerName + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
                              End Try
                      
                          End Function

Open in new window


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()
                                      System.Console.WriteLine("Share has been removed on : " + computername)
                                      System.Console.WriteLine("ShareName : " + shareName)
                                  Next
                              Catch ex As Exception
                                  System.Console.Write("Error occured 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)
                                  System.Console.WriteLine("Computername : " + computername + "   ShareName : " + shareName)
                              End Try
                      
                          End Function

Open in new window


Source Files (VB.NET 2008)

Download Source Remote Share GUI
Download Source Remote Share Console

Blogs
BlogSpot
CodeProject

Public Class frmRemoteSharing
                      
                          Public ComputerName As String
                          Public ShareName As String
                          Public SharePath As String
                          Public ShareInfo As String
                      
                          Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                              If Not ComputerName = "" Then
                                  ShowShares(ComputerName)
                              End If
                          End Sub
                      
                          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 aready shared")
                                  End If
                              Catch ex As Exception
                                  MsgBox("Error occured 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
                          '
                          Public Function ShowShares(ByVal ComputerName) 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 occured 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
                      
                          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 occured 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
                      
                          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                              If Not ComputerName = "" And Not ShareName = "" And Not SharePath = "" Then
                                  CreateShare(ShareName, SharePath, ComputerName, ShareInfo)
                              End If
                              ShowShares(ComputerName)
                          End Sub
                      
                          Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
                              ComputerName = TextBox1.Text
                          End Sub
                      
                          Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
                              ShareName = TextBox2.Text
                          End Sub
                      
                          Private Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
                              SharePath = TextBox3.Text
                          End Sub
                      
                          Private Sub TextBox4_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
                              ShareInfo = TextBox4.Text
                          End Sub
                      
                          Private Sub frmRemoteSharing_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                      
                              ComputerName = TextBox1.Text
                              ShareName = TextBox2.Text
                              SharePath = TextBox3.Text
                              ShareInfo = TextBox4.Text
                          End Sub
                      
                          Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
                              If Not ListView1.SelectedItems(0).Text = "" Then
                                  RemoveShare(ListView1.SelectedItems(0).Text, ComputerName)
                              End If
                              ShowShares(ComputerName)
                          End Sub
                      
                          Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
                              Shell("explorer.exe \\" + ComputerName + "\" + ListView1.SelectedItems(0).Text, AppWinStyle.NormalFocus)
                          End Sub
                      End Class
                      

Open in new window

Module modMain
                      
                          Public ComputerName As String
                          Public ShareName As String
                          Public SharePath As String
                          Public ShareInfo As String
                      
                          Sub main()
                              Dim i As Integer = 0
                      
                              For Each Argument As String In My.Application.CommandLineArgs
                                  If i = 0 Then
                                      Select Case LCase(Argument)
                      
                                          Case "-?"
                      
                                              System.Console.WriteLine("Remote Share : Version : " + My.Application.Info.Version.ToString)
                                              System.Console.WriteLine("################################")
                                              System.Console.WriteLine(vbCrLf)
                                              System.Console.WriteLine("Remote Share can be used to show, create and remove shares on a remote or local computer.")
                                              System.Console.WriteLine(vbCrLf)
                                              System.Console.WriteLine("Usage : RemoteShare -?")
                                              System.Console.WriteLine("Displays this help information")
                                              System.Console.WriteLine(vbCrLf)
                                              System.Console.WriteLine("Usage : RemoteShare -show COMPUTERNAME")
                                              System.Console.WriteLine("Displays all shares on specific computer")
                                              System.Console.WriteLine(vbCrLf)
                                              System.Console.WriteLine("Usage : RemoteShare -create COMPUTERNAME ShareName SharePath ShareInfo")
                                              System.Console.WriteLine("Creates a share on remote or local computer")
                                              System.Console.WriteLine(vbCrLf)
                                              System.Console.WriteLine("Usage : RemoteShare -remove COMPUTERNAME ShareName")
                                              System.Console.WriteLine("Removes a share on remote or local computer")
                                              System.Console.WriteLine(vbCrLf)
                                              System.Console.WriteLine("Example : RemoteShare -create COMPUTER1 C$ C:\ " + Chr(34) + "Temporary IT Share" + Chr(34))
                      
                                          Case "-show"
                                              Dim j As Integer = 0
                                              For Each arg As String In My.Application.CommandLineArgs
                                                  If j = 1 Then
                                                      ComputerName = arg
                                                      ShowShares(ComputerName)
                                                      Exit Sub
                                                  End If
                                                  j = j + 1
                                              Next
                                          Case "-create"
                                              Dim j As Integer = 0
                                              For Each arg As String In My.Application.CommandLineArgs
                                                  If j = 1 Then
                                                      ComputerName = arg
                                                  ElseIf j = 2 Then
                                                      ShareName = arg
                                                  ElseIf j = 3 Then
                                                      SharePath = arg
                                                  ElseIf j = 4 Then
                                                      ShareInfo = arg
                                                  End If
                                                  j = j + 1
                                              Next
                                              CreateShare(ShareName, SharePath, ComputerName, ShareInfo)
                                              Exit Sub
                                          Case "-remove"
                                              Dim j As Integer = 0
                                              For Each arg As String In My.Application.CommandLineArgs
                                                  If j = 1 Then
                                                      ComputerName = arg
                                                  ElseIf j = 2 Then
                                                      ShareName = arg
                                                  End If
                                                  j = j + 1
                                              Next
                                              RemoveShare(ShareName, ComputerName)
                                          Case Else
                                              System.Console.WriteLine("Unknown argument supplied")
                                              Exit Sub
                                      End Select
                      
                                  End If
                                  i = i + 1
                                  Exit Sub
                              Next
                      
                          End Sub
                      
                      
                      
                          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)
                                      System.Console.WriteLine("Share has been created on : " + computername)
                                      System.Console.WriteLine("ShareName : " + ShareName + "   SharePath : " + SharePath + "   ShareInfo : " + shareinfo)
                                  Else
                                      System.Console.WriteLine("Share is already present on computer : " + computername)
                                      System.Console.WriteLine("ShareName : " + ShareName + "   SharePath : " + SharePath + "   ShareInfo : " + shareinfo)
                                  End If
                              Catch ex As Exception
                                  System.Console.Write("Error occured while trying to create shares on remote pc : " + computername + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
                                  System.Console.WriteLine("ShareName : " + ShareName + "   SharePath : " + SharePath + "   ShareInfo : " + shareinfo)
                              End Try
                      
                          End Sub
                          '
                          Public Function ShowShares(ByVal ComputerName) As Boolean
                              Try
                      
                                  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 
                                  System.Console.WriteLine("Remote Shares on : " + ComputerName)
                                  For Each objSWbem In colSWbemObject
                                      System.Console.WriteLine("ShareName : " + objSWbem.name + "   SharePath : " + objSWbem.path + "   ShareInfo : " + objSWbem.description)
                                  Next
                              Catch ex As Exception
                                  System.Console.Write("Error occured while trying to show shares on remote pc : " + ComputerName + vbCrLf + "Check if you have the necessary rights and/or that the pc is turned on." + vbCrLf + ex.ToString)
                              End Try
                      
                          End Function
                      
                          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()
                                      System.Console.WriteLine("Share has been removed on : " + computername)
                                      System.Console.WriteLine("ShareName : " + shareName)
                                  Next
                              Catch ex As Exception
                                  System.Console.Write("Error occured 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)
                                  System.Console.WriteLine("Computername : " + computername + "   ShareName : " + shareName)
                              End Try
                      
                          End Function
                      
                      
                      End Module
                      

Open in new window

1
5,483 Views

Comments (2)

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Great article!!  You might want to think about looking at the System.Management namespace, and the classes, such as the ManagementObjectSearcher, ManagementObject, etc.

Author

Commented:
Yes that would indeed be an other option, this was actually an vbscript I adapted for VB.NET.

But that might be a good follow up article...

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.