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

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))
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/2004 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function GetShares() As String()
Return GetShares(".")
End Function

''' -----------------------------------------------------------------------------
''' <summary>
''' Stops sharing the folder.
''' </summary>
''' <history>
''' [Eduardo Morcillo] 11/08/2004 Created
''' </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/2004 Created
''' </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/2004 Created
''' </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

时间: 2024-12-26 08:47:32

VB.NET下通过WMI共享文件夹的相关文章

Win10在局域网下如何设置共享文件夹

首先要找到共享的文件,如下图用资源管理器打开 在你要共享的文件夹上点右键然后再点[共享]---[特定用户] 在这里选择一个要添加的用户或是组.点击[添加] 添加你想共享的用户名,如果向所有的用户开放则需要添加[Everyone]这个用户.据需要调整权限级别 点击共享按钮,在出现的对话框中启用网络发现和共享 进行测试win+R输入:\\IP地址   就把该ip地址所有共享的都显示出来了.

VirtualBox下CentOS共享文件夹

  在VirtualBox虚拟机中安装CentOS,设置与win7共享文件夹的方法: 1. 在VirtualBox增强工具安装文件夹共享工具,然后按照提示一步步并在windows下设置一个共享文件夹. 2. 进入CentOS虚拟机,启用共享,会发现桌面有一个光盘,这是挂载在/media下的.进入/media下的该光盘目录,执行run文件. [root@localhost media]# cd /media/VBOXADDITIONS_4.3.10_93012 [root@localhost VB

Win7下取消共享文件夹上的小锁图标

  我们用过Windows 7系统的都会发现,共享后的文件夹图标左下角显示锁,取消共享后依然存在,如图: Window 7系统中小锁其实是开启简单共享后,文件本身访问权限受限的标识.添加所有用户权限后可以消失. 操作步骤: 一.所需调整文件夹图标上单击鼠标右键,移动至"共享",点击"特定用户共享",出现文件共享设置窗口,在下拉菜单中选择"Everyone"---选择"添加"; 二.点击"共享"后点击&quo

Win7下取消共享文件夹上的小锁图标方法

  我们用过Windows 7系统的都会发现,共享后的文件夹图标左下角显示锁,取消共享后依然存在,如图: Window 7系统中小锁其实是开启简单共享后,文件本身访问权限受限的标识.添加所有用户权限后可以消失. 操作步骤: 一.所需调整文件夹图标上单击鼠标右键,移动至"共享",点击"特定用户共享",出现文件共享设置窗口,在下拉菜单中选择"Everyone"---选择"添加"; 二.点击"共享"后点击&quo

清除本机在Windows下访问共享文件夹

  不知道大家遇到过这种情况没有,呵呵,我是经常遇到,虽然知道有一种办法,但是有点麻烦,于是从网络上另找到一个方法来解决它. 当我们访问局域网里的一台主机的共享文件夹时,首次登录会要求输入用户名和密码,当输入正确后,即使没有勾选"Remember my credentials"选项,下次输入地址也会自动登录到那个文件夹里.虽然这个功能方便了我再次输入登录信息的麻烦,但是当我想切换权限更大的用户时就带来了一点小麻烦.呵呵,可能会有人有过这种经历吧,至少证明我还有同类吧,呵呵. 一.在没有

在域环境下,如何让普通用户(domain user)也能有共享文件夹的权限?

这是我今天偶然间想到的,在网上找了找相关的资料. 如下:默认情况下,在单机或工作组环境中,只有Administrators组.Power Users组的成员才有权共享文件夹:在域环境中,只有Administrators组.Server Operators组才有权限共享文件夹. 如果要进行共享的话,那么在工作组环境下把你的用户加入Administrators组或Power Users组:而在域的环境下:在哪台要共享的计算机上的Power Users组中添加Domain Users组: 具体的相关操

用vb.net自定义控件,可以选择文件夹下的图片并显示图片的名称,怎么做

问题描述 用vb.net自定义控件,可以选择文件夹下的图片并显示图片的名称,怎么做 急求!!!就是就是从文件夹中可以选择图片,但下面要获取图片的名字,比如11.jpg,22.jpg.这不是图片的名字么,要和图片信息一致.从文件夹中选择图片,要用什么语句?是用filename获取文件的名字么,我是菜鸟,完全不知道怎么写〉〉〉...... 解决方案 用openfiledialog 参考:http://www.poluoluo.com/jzxy/200809/45925.htmlhttp://www.

win7 共享文件-哪位大神解释下:win7共享文件夹问题,

问题描述 哪位大神解释下:win7共享文件夹问题, 哪位大神解释下:计算机win7名称:FLASH-PC : 为什么FLASH-PC可以访问本机共享文件,而127.0.0.1却找不到网络路径

Vmware 下共享文件夹(Windows到Linux)

      共享Windows下的文件夹给Vmware下的虚拟机变得越来越简单,且易于实现.安装好虚拟机后,首先安装Vmware-tools工具包,然后再来配置文件夹的共享功能.本文直接通过图文演示Windows 7(64bit) + Vmware 9.0.2下配置共享文件夹给Suse Linux 10访问.   1.安装Vmware-tools工具包     安装好客户机后,VM菜单下有一个Reinstall VMware Tools,直接单击,该工具包被加载到客户机桌面     命令行模式下