分享
 
 
 

VB.NET下通过WMI共享文件夹

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

''' </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/2004Created

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

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 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 shared resources in the local computer.

''' </summary>

''' <returns></returns>

''' <remarks>

''' </remarks>

''' <history>

''' [Eduardo Morcillo]11/08/2004Created

''' </history>

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

Public Shared Function GetShares() As String()

Return GetShares(".")

End Function

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

''' <summary>

''' Stops sharing the folder.

''' </summary>

''' <history>

''' [Eduardo Morcillo]11/08/2004Created

''' </history>

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

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 the WMI object

_share.Dispose()

GC.SuppressFinalize(Me)

End Sub

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

''' <summary>

''' Sets the share info.

''' </summary>

''' <remarks>The security descriptor is not supported by this class.</remarks>

''' <history>

''' [Eduardo Morcillo]11/08/2004Created

''' </history>

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

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>

''' <history>

''' [Eduardo Morcillo]11/08/2004Created

''' </history>

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

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

<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

http://www.applevb.com

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有