Vb. NET through WMI shared folders

Source: Internet
Author: User
Tags integer modify resource tostring
Option Explicit on
Option Strict on

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Globalization
Imports System.Management

Namespace Edanmo.io

Public NotInheritable Class NetShare
Implements IDisposable

Private _share as ManagementObject

Private Sub New (ByVal share as ManagementObject)
_share = Share
End Sub

''' -----------------------------------------------------------------------------
"' <summary>
"Gets the share access rights for the" Current user or group.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public ReadOnly Property AccessMask () as Accessmasks
Get
Return CType (Convert.ToInt32 (_share). InvokeMethod ("Getaccessmask", Nothing), CultureInfo.InvariantCulture), accessmasks)
End Get
End Property

''' -----------------------------------------------------------------------------
"' <summary>
' Gets or sets the maximum number of user connections.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Property AllowMaximum () as Integer
Get
Return Convert.ToInt32 (_share. GetPropertyValue ("AllowMaximum"), CultureInfo.InvariantCulture)
End Get
Set (ByVal value as Integer)
Me.setshareinfo (value, me.description, nothing)
End Set
End Property

''' -----------------------------------------------------------------------------
"' <summary>
"' Gets the share description.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Property Description () as String
Get
Return _share. GetPropertyValue ("Description"). Tostring
End Get
Set (ByVal value as String)
Me.setshareinfo (me.maximumallowed, value, nothing)
End Set
End Property

''' -----------------------------------------------------------------------------
"' <summary>
"' Gets
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Property maximumallowed () as Integer
Get
Return Convert.ToInt32 (_share. GetPropertyValue ("maximumallowed"), CultureInfo.InvariantCulture)
End Get
Set (ByVal value as Integer)
Me.setshareinfo (value, me.description, nothing)
End Set
End Property

''' -----------------------------------------------------------------------------
"' <summary>
"" Gets the share name.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public ReadOnly Property Name () as String
Get
Return _share. GetPropertyValue ("Name"). Tostring
End Get
End Property

''' -----------------------------------------------------------------------------
"' <summary>
"" Gets the local path of the share.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public ReadOnly Property Path () as String
Get
Return _share. GetPropertyValue ("Path"). Tostring
End Get
End Property

''' -----------------------------------------------------------------------------
"' <summary>
"' Gets the share status.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public ReadOnly Property Status () as String
Get
Return _share. GetPropertyValue ("Status"). Tostring
End Get
End Property

''' -----------------------------------------------------------------------------
"' <summary>
"" Gets the share type.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public ReadOnly Property Type () as Sharetype
Get

Dim typeValue64 as Long = Convert.toint64 (_share. GetPropertyValue ("Type"), CultureInfo.InvariantCulture)
Dim TypeValue32 as Integer

If (TypeValue64 and &h80000000) > 0 Then
TypeValue32 = &h80000000 Or convert.toint32 (typeValue64 and &h7fffffff, CultureInfo.InvariantCulture)
Else
TypeValue32 = Convert.ToInt32 (typeValue64, CultureInfo.InvariantCulture)
End If

Return CType (TYPEVALUE32, Sharetype)

End Get
End Property

''' -----------------------------------------------------------------------------
"' <summary>
' Creates a shared folder in the local computer.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Shared Function Create (_
ByVal path as String, _
ByVal name as String) as NetShare

Return Create (".", Path, sharetype.diskdrive, name,-1, no, nothing)

End Function

''' -----------------------------------------------------------------------------
"' <summary>
' Creates a shared folder in the local computer.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Shared Function Create (_
ByVal path as String, _
ByVal name as String, _
ByVal Password as String) as NetShare

Return Create (".", Path, sharetype.diskdrive, name,-1, nothing, password)

End Function

''' -----------------------------------------------------------------------------
"' <summary>
' Creates a shared folder in the local computer.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Shared Function Create (_
ByVal path as String, _
ByVal type as Sharetype, _
ByVal name as String, _
ByVal maximumallowed as Integer, _
ByVal description as String, _
ByVal Password as String) as NetShare

Return Create (".", Path, type, name, maximumallowed, description, password)

End Function

''' -----------------------------------------------------------------------------
"' <summary>
' Creates a shared resource in the specified computer.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Shared Function Create (_
ByVal ComputerName as String, _
ByVal path as String, _
ByVal type as Sharetype, _
ByVal name as String, _
ByVal maximumallowed as Integer, _
ByVal description as String, _
ByVal Password as String) as NetShare

Dim Shareclass as New System.Management.ManagementClass (String.Format ("\\{0}\root\cimv2:win32_share", ComputerName) )
Dim Res as Integer

Try

If maximumallowed < 0 Then

Res = Convert.ToInt32 (_
Shareclass.invokemethod ("Create", _
New Object () {path, name, type, nothing, description, password, nothing}, CultureInfo.InvariantCulture)

Else

Res = Convert.ToInt32 (_
Shareclass.invokemethod ("Create", _
New Object () {path, name, type, maximumallowed, description, password, nothing}, CultureInfo.InvariantCulture)

End If

If Res <> 0 Then throwexception (res)

Return Getshare (computerName, name)

Finally

Shareclass.dispose ()

End Try

End Function

''' -----------------------------------------------------------------------------
"' <summary>
"" Returns a NetShare object that represents the shared resource in the
"Specified computer."
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Shared Function Getshare (ByVal computerName As String, ByVal sharename as String) as NetShare

Dim Share as ManagementObject

Share = New ManagementObject (String.Format ("\\{0}\root\cimv2:win32_share.name=" "{1}" ", ComputerName, sharename))
Share. Get ()

return New NetShare (share)

End Function

''' -----------------------------------------------------------------------------
"' <summary>
"' Returns a NetShare object that represents the shared resource.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Shared Function Getshare (ByVal sharename as String) as NetShare

Return Getshare (".", ShareName)

End Function

''' -----------------------------------------------------------------------------
"' <summary>
"" Returns the names of the specified computer.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Shared Function getshares (ByVal ComputerName As String) as String ()

' Get the Win32_Share class
Dim Shareclass as New System.Management.ManagementClass (String.Format ("\\{0}\root\cimv2:win32_share", computername) )
Dim shares as Managementobjectcollection

Try

Dim Sharenames as New ArrayList

' Get the Win32_Share instances
Shares = Shareclass.getinstances

' Enumerate all instances
For each share as ManagementObject in shares

Try

' Add the name to the ' list
Sharenames.add (share. GetPropertyValue ("Name"))

Finally

' Release the WMI object
Share. Dispose ()

End Try

Next

' Return ' to the list as an array
Return DirectCast (Sharenames.toarray (GetType (String)), String ())

Finally

' Release the WMI object
Shareclass.dispose ()

End Try

End Function

''' -----------------------------------------------------------------------------
"' <summary>
"" Returns the names of the "shared resources" local computer.
"' </summary>
"' <returns></returns>
"' <remarks>
"' </remarks>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Shared Function Getshares () as String ()
Return Getshares (".")
End Function

''' -----------------------------------------------------------------------------
"' <summary>
"' Stops sharing the folder.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Public Sub Delete ()

Dim Res as Integer

res = Convert.ToInt32 (_share. InvokeMethod ("Delete", Nothing), CultureInfo.InvariantCulture)

If Res <> 0 Then throwexception (res)

End Sub

Public Sub Dispose () Implements System.IDisposable.Dispose

' Dispose of the WMI object
_share. Dispose ()

Gc. SuppressFinalize (Me)

End Sub

''' -----------------------------------------------------------------------------
"' <summary>
"' Sets the share info.
"' </summary>
"' <remarks>the security descriptor are not supported by this class.</remarks>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Private Sub Setshareinfo (_
ByVal maximumallowed as Integer, _
ByVal description as String, _
ByVal Descriptor as Managementbaseobject)

Dim Res as Integer

' Set the share info
res = Convert.ToInt32 (_share. InvokeMethod ("Setshareinfo", New Object () {maximumallowed, description, descriptor}), CultureInfo.InvariantCulture)

If Res <> 0 Then throwexception (res)

End Sub

''' -----------------------------------------------------------------------------
"' <summary>
' Throws an exception for the specified WMI error number.
"' </summary>
"' "[Eduardo Morcillo] 11/08/2004 Created
"' ''' -----------------------------------------------------------------------------
Private Shared Sub throwexception (ByVal res as Integer)

Select Case Res

Case 2 ' Access denied
Throw New Win32Exception (65)
Case 9 ' Invalid name
Throw New win32exception (1215)
Case a ' Invalid level
Throw New win32exception (124)
Case ' Invalid parameter
Throw New Win32Exception (87)
Case ' Duplicate Share
Throw New win32exception (2118)
Case ' redirected Path
Throw New win32exception (2117)
Case ' Unknown device Or directory
Throw New win32exception (2116)
Case ' Net name not found
Throw New Win32Exception (67)
Case Else
Throw New Exception ("Unknown Error:" & Res)

End Select

End Sub

End Class

Public Enum Sharetype as Integer
diskdrive = 0
PrintQueue = 1
Device = 2
IPC = 3
Diskdriveadmin = &h80000000
Printqueueadmin = &h80000001
Deviceadmin = &h80000002
Ipcadmin = &h80000003
End Enum

<flags () > Public Enum accessmasks as Integer
ListDirectory = &h1
AddFile = &h2
Addsubdirectory = &h4
Readextendedattributes = &h8
Writeextendedattributes = &h10
Traverse = &h20
Deletechild = &h40
ReadAttributes = &h80
WriteAttributes = &h100
Delete = &h10000
Readcontrol = &h20000
WRITEDAC = &h40000
Writeowner = &h80000
Synchronize = &h100000

Read = ListDirectory or readextendedattributes or readattributes or Readcontrol or Synchronize
Readandexecute = Read Or Traverse
Write = AddFile or addsubdirectory or writeextendedattributes or WriteAttributes or Synchronize
Modify = Read or Write or Delete or Traverse
FullControl = Deletechild or WriteDAC or Writeowner or Modify

End Enum

End Namespace



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.