通过WMI共享文件夹

2007-12-06  来源:   浏览次数 2
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property Path() As String
Get
Return _share.GetPropertyValue("Path").ToString
End Get
End Property

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

''' -----------------------------------------------------------------------------
''' <summary>
''' Gets the share type.
''' </summary>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function Create( _
ByVal path As String, _
ByVal name As String) As NetShare

Return Create(".", path, ShareType.DiskDrive, name, -1, Nothing, Nothing)

End Function

''' -----------------------------------------------------------------------------
''' <summary>
''' Creates a shared folder in the local computer.
''' </summary>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function GetShare(ByVal shareName As String) As NetShare

Return GetShare(".", shareName)

End Function

''' -----------------------------------------------------------------------------
''' <summary>
''' Returns the names of shared resources in the specified computer.
''' </summary>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
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))

相关主题:

网友评论