首页 > 编程 > .NET > 正文

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

2024-07-10 13:00:44
字体:
来源:转载
供稿:网友

最大的网站源码资源下载站,

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表