Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. share and unshare folder

share and unshare folder

Scheduled Pinned Locked Moved Visual Basic
help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mehrnoosh
    wrote on last edited by
    #1

    hi friends. i am writting a share and unshare program and i use this code for share a folder

    Private Sub Sharing()
    Try

            Dim managementClass As New ManagementClass("Win32\_Share")
            Dim inParams As ManagementBaseObject = managementClass.GetMethodParameters("Create")
            inParams("Description") = fname
            inParams("Name") = fname
            inParams("Path") = fpath
            inParams("Type") = &H0
    
          
                Dim outParams As ManagementBaseObject = managementClass.InvokeMethod("Create", inParams, Nothing)
                If Convert.ToUInt32(outParams.Properties("ReturnValue").Value) <> 0 Then
                    chkShare.Checked = True
                End If '
            
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    

    this works successfully but now i dont know how i can unshare a folder... help me please

    M.Alizadeh

    D 1 Reply Last reply
    0
    • M mehrnoosh

      hi friends. i am writting a share and unshare program and i use this code for share a folder

      Private Sub Sharing()
      Try

              Dim managementClass As New ManagementClass("Win32\_Share")
              Dim inParams As ManagementBaseObject = managementClass.GetMethodParameters("Create")
              inParams("Description") = fname
              inParams("Name") = fname
              inParams("Path") = fpath
              inParams("Type") = &H0
      
            
                  Dim outParams As ManagementBaseObject = managementClass.InvokeMethod("Create", inParams, Nothing)
                  If Convert.ToUInt32(outParams.Properties("ReturnValue").Value) <> 0 Then
                      chkShare.Checked = True
                  End If '
              
          Catch ex As Exception
              MessageBox.Show(ex.Message)
          End Try
      End Sub
      

      this works successfully but now i dont know how i can unshare a folder... help me please

      M.Alizadeh

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You already know how to call the Create method on the Win32_Share class, so what's so hard about calling its Delete method? All you do is a WMI search for Win32_Share object with the Name you want, then call its Delete method. You may want to consult the documentation on Win32_Share[^]for more.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008
      But no longer in 2009...

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You already know how to call the Create method on the Win32_Share class, so what's so hard about calling its Delete method? All you do is a WMI search for Win32_Share object with the Name you want, then call its Delete method. You may want to consult the documentation on Win32_Share[^]for more.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008
        But no longer in 2009...

        M Offline
        M Offline
        mehrnoosh
        wrote on last edited by
        #3

        thanks for your answer i searched many articles but i dont know how use Delete Method of the Win32_Share Class when i use this cose:

        Dim dir As String = fpath
        
            Directory.CreateDirectory(dir)
        
            'Create a ManagementClass Object
        
            Dim managementClass As New ManagementClass("Win32\_Share")
        
            ' Create ManagementBaseObjects for in and out parameters
        
            Dim inParams As ManagementBaseObject = managementClass.GetMethodParameters("Delete")
        
            Dim outParams As ManagementBaseObject
        
            ' Set the input parameters
        
            inParams("Description") = fname
        
            inParams("Name") = fname
            inParams("Path") = dir
        
            inParams("Type") = &H0
        
            ' Invoke the method on the ManagementClass object
        
            outParams = managementClass.InvokeMethod("Create", inParams, Nothing)
        
            'Confirm that the operation is successful
        
            If CUInt(outParams.Properties("ReturnValue").Value) <> 0 Then
        
        
        
                Throw New Exception("Unable to share the directory.")
            End If
        
            MessageBox.Show("Folder share deleted")
        

        in this line: "inParams("Description") = fname" it has a error:Object reference not set to an instance of an object. fname is not null and has value

        M.Alizadeh

        A D 2 Replies Last reply
        0
        • M mehrnoosh

          thanks for your answer i searched many articles but i dont know how use Delete Method of the Win32_Share Class when i use this cose:

          Dim dir As String = fpath
          
              Directory.CreateDirectory(dir)
          
              'Create a ManagementClass Object
          
              Dim managementClass As New ManagementClass("Win32\_Share")
          
              ' Create ManagementBaseObjects for in and out parameters
          
              Dim inParams As ManagementBaseObject = managementClass.GetMethodParameters("Delete")
          
              Dim outParams As ManagementBaseObject
          
              ' Set the input parameters
          
              inParams("Description") = fname
          
              inParams("Name") = fname
              inParams("Path") = dir
          
              inParams("Type") = &H0
          
              ' Invoke the method on the ManagementClass object
          
              outParams = managementClass.InvokeMethod("Create", inParams, Nothing)
          
              'Confirm that the operation is successful
          
              If CUInt(outParams.Properties("ReturnValue").Value) <> 0 Then
          
          
          
                  Throw New Exception("Unable to share the directory.")
              End If
          
              MessageBox.Show("Folder share deleted")
          

          in this line: "inParams("Description") = fname" it has a error:Object reference not set to an instance of an object. fname is not null and has value

          M.Alizadeh

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          mehrnoosh wrote:

          i searched many articles but i dont know how use Delete Method of the Win32_Share Class

          Try the documentation on MSDN. It's always a good place to start. Win32-Share[^] and Delete method[^] Alan.

          1 Reply Last reply
          0
          • M mehrnoosh

            thanks for your answer i searched many articles but i dont know how use Delete Method of the Win32_Share Class when i use this cose:

            Dim dir As String = fpath
            
                Directory.CreateDirectory(dir)
            
                'Create a ManagementClass Object
            
                Dim managementClass As New ManagementClass("Win32\_Share")
            
                ' Create ManagementBaseObjects for in and out parameters
            
                Dim inParams As ManagementBaseObject = managementClass.GetMethodParameters("Delete")
            
                Dim outParams As ManagementBaseObject
            
                ' Set the input parameters
            
                inParams("Description") = fname
            
                inParams("Name") = fname
                inParams("Path") = dir
            
                inParams("Type") = &H0
            
                ' Invoke the method on the ManagementClass object
            
                outParams = managementClass.InvokeMethod("Create", inParams, Nothing)
            
                'Confirm that the operation is successful
            
                If CUInt(outParams.Properties("ReturnValue").Value) <> 0 Then
            
            
            
                    Throw New Exception("Unable to share the directory.")
                End If
            
                MessageBox.Show("Folder share deleted")
            

            in this line: "inParams("Description") = fname" it has a error:Object reference not set to an instance of an object. fname is not null and has value

            M.Alizadeh

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            You didn't bother reading the documentation in the link I gave you in my last post, did you? There are no parameters to pass to the Delete method, so why are you calling GetMethodParameters?? What are you trying to set parameters?? Why are you still calling InvokeMethod on the Create method?? I already told you you need to SEARCH for a Win32_Share object with the NAME you want. You then call the Delete method on that instance.

            mehrnoosh wrote:

            in this line: "inParams("Description") = fname" it has a error:Object reference not set to an instance of an object.

            Of course it errors! Why would you set a Description on an object you're trying to delete?! It's obvious that you don't have any idea what a single line of this code does or how it relates to the idea of creating a Share let alone deleting one. Having said that, I have to ask if you know how to do a WMI search for an object?

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008
            But no longer in 2009...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups