VB.NET with WMI shared folder

xiaoxiao2021-03-06  40

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

'' ----------------------------------------------- ------------------------------

'' '

'' 'Gets The Share Access Rights for The Current User or Group.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public Readonly Property Accessmask () As Accessmasks

Get

Return CType (_Share.invokeMethod ("GetAccessmask", Nothing, CultureInfo.invariantculture, Accessmasks)

END GET

End Property

'' ----------------------------------------------- ------------------------------

'' '

'' 'Gets or sets the maximum number of user connections.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

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

'' ----------------------------------------------- ---------------------------- "''

'' 'Gets the share description.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public property description () AS STRING

Get

Return _Share.getPropertyValue ("description"). Tostring

END GET

Set (byval value as string)

Me.setshareInfo (Me.maximumlowed, Value, Nothing)

End set

End Property

'' ----------------------------------------------- ------------------------------

'' '

'' Gets

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

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

'' ----------------------------------------------- ------------------------------

'' '

'' Gets the share name.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public Readonly Property Name () AS STRINGET

Return _Share.getPropertyValue ("name"). Tostring

END GET

End Property

'' ----------------------------------------------- ------------------------------

'' '

'' Gets the Local path of the share.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public Readonly Property Path () AS String

Get

Return_Share.getPropertyValue ("Path"). Tostring

END GET

End Property

'' ----------------------------------------------- ------------------------------

'' '

'' Gets the share status.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public Readonly Property Status () AS STRING

Get

Return _Share.getPropertyValue ("status"). Tostring

END GET

End Property

'' ----------------------------------------------- ------------------------------

'' '

'' 'Gets the share type.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

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 & H7FFFFFFFF, CULTUREINFO.INVARIANTCULTURE)

Else

TypeValue32 = Convert.Toint32 (TypeValue64, CultureInfo.invariantculture)

END IF

Return CType (TypeValue32, Sharetype)

END GET

End Property

'' ----------------------------------------------- ------------------------------

'' '

'' 'Creates a Shared Folder in The Local Computer.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

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

'' ----------------------------------------------- ------------------------------

'' '

'' 'Creates a Shared Folder in The Local Computer.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

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 '' '--------------------------------------------- --------------------------------

'' '

'' 'Creates a Shared Folder in The Local Computer.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public Shared Function Create (_

Byval path as string, _

Byval Type as Sharetype, _

Byval Name as string, _

Byval maximumowed as integer, _

Byval Description as string, _

Byval Password As String) AS Netshare

Return Create (".", Path, Type, Name, Maximumlowed, Description, Password

END FUNCTION

'' ----------------------------------------------- ------------------------------

'' '

'' 'Creates A Shared Resource in The Specified Computer.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public Shared Function Create (_

Byval computename as string, _

Byval path as string, _

Byval Type as Sharetype, _

Byval Name as string, _

Byval maximumowed 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", computename))

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 THROWEXCEPTION (RES)

Return getShare (ComputerName, Name)

Finally

Shareclass.dispose ()

END TRY

END FUNCTION

'' ----------------------------------------------- ------------------------------

'' '

'' Returns a Netshare Object That Repesents The Shared Resource in The

'' 'Specified Computer.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public Shared Function GetShare (Byval ComputerName As String) AS Netshare

Dim Share As ManagementObject

Share = new management (String.Format ("// {0} /Root/cimv2:Win32_share.name =" {1} "" ", computename, sharename))

Share.get ()

Return New Netshare (Share)

END FUNCTION

'' ----------------------------------------------- ------------------------------

'' '

'' Returns a Netshare Object this represents the shared resource.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public Shared Function GetShare (Byval Sharename As String) AS Netsharereturn getShare (".", Sharename)

END FUNCTION

'' ----------------------------------------------- ------------------------------

'' '

'' 'Returns the names of shared resources in the specified computer.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

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", computename))

Dim Sharees as managementObjectCollection

Try

Dim Shasenames as new arraylist

'Get the win32_share instances

Shares = sharclass.getinstances

'ENUMERATE ALL Instances

For Each Share As ManagementObject in Shars

Try

'Add the name to the list

ShareNames.Add (Share.GetPropertyValue ("name"))

Finally

'Release the WMI Object

Share.dispose ()

END TRY

NEXT

'Return the List as an Array

Return Directcast (Shasenames.toArray (Gettype (String), String ())

Finally

'Release the WMI Object

Shareclass.dispose ()

END TRY

END FUNCTION

'' ----------------------------------------------- ------------------------------

'' '

'' Returns The names of shared resources in the local computer.

''

'' '

'' '

'' '' ''

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public Shared Function Getshars () AS String ()

Return getshares (".")

END FUNCTION

'' ----------------------------------------------- ------------------------------

'' '

'' Stops Sharing The Folder.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Public SUB Delete ()

DIM RES AS INTEGER

Res = Convert.Toint32 (_Share.InvokeMethod ("delete", nothing), CultureInfo.invariantculture)

IF res <> 0 THROWEXCEPTION (RES)

End Sub

Public Sub Dispose () IMPLEments System.idisposable.dispose

'Dispose the WMI Object

_Share.dispose ()

Gc.suppressFinalize (ME)

End Sub

'' ----------------------------------------------- ------------------------------

'' '

'' 'Sets the share info.

''

'' The security descriptor is not supported by this class.

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

Private sub setshareinfo (_

Byval maximumowed 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 () {maximum}, cultureinfo.invariantculture

IF res <> 0 THROWEXCEPTION (RES)

End Sub

'' ----------------------------------------------- ------------------------------

'' '

'' 'Throws an Exception for the Specified WMI Error Number.

''

'' '

'' '[Eduardo morcillo] 11/08/2004 create

'' '

'' ----------------------------------------------- ------------------------------

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 10 'INVALID LEVEL

Throw new win32exception (124)

Case 21 'Invalid Parameter

Throw New Win32Exception (87)

Case 22 'DuPlicate Share

Throw new win32exception (2118)

Case 23 'Redirected Path

Throw New Win32Exception (2117)

Case 24 'Unknown Device or Directory

Throw New Win32Exception (2116)

Case 25 '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

Public Enum Accessmasks as in Integer

Listdirectory = & h1addfile = & h2

AddsubDirectory = & H4

ReadextendedAttributes = & H8

WriteextendedAttributes = & H10

Traverse = & H20

DeleteChild = & H40

Readttributes = & 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 Travers

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

http://www.applevb.com

转载请注明原文地址:https://www.9cbs.com/read-66331.html

New Post(0)