VB.net 공유폴더 만들기

교교 2014.02.24 19:49:51 *: 먼저 참조(레퍼런스)를 추가시켜주어야 함 System.Management


Imports System.Management

Try

Dim managementClass As New ManagementClass("Win32_Share")
Dim inParams As ManagementBaseObject = managementClass.GetMethodParameters("Create")
inParams("Description") = "My Description"
inParams("Name") = "Share Name"
inParams("Path") = "C:\My Folder"
inParams("Type") = &H0

Dim outParams As ManagementBaseObject = managementClass.InvokeMethod("Create", inParams, Nothing)
If Convert.ToUInt32(outParams.Properties("ReturnValue").Value) <> 0 Then
MessageBox.Show("Unable to share directory.")
else
MessageBox.Show("Shared folder successfully!")
End If

Catch ex As Exception
MessageBox.Show(ex.Message)

End Try



' 공유폴더 권한 설정
Imports System.Security.AccessControl
Imports System.IO

Public Class Form1

' Add access control for Windows Account to directory or file
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddFileSecurity("\\MartinXie\ShareFolder", "Everyone", "FullControl")
AddFileSecurity("\\MartinXie\ShareFolder", "Domain\UserCccount", "Write")
End Sub

Public Sub AddFileSecurity(ByVal filePath As String, ByVal username As String, ByVal power As String)
Dim dirinfo As DirectoryInfo = New DirectoryInfo(filePath)
Dim dirsecurity As DirectorySecurity = dirinfo.GetAccessControl()
Select Case power
Case "FullControl"
dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))
dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow))
dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))
Case "ReadOnly"
dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow))
Case "Write"
dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))
dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow))
dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))
Case "Modify"
dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow))
End Select
dirinfo.SetAccessControl(dirsecurity)
End Sub
End Class

.