Create a Directory as a share using .net code
At first create a class with name Win32Share and write the following code :
Imports System.Text
Imports System.Management
Class Win32Share
Public Enum MethodStatus As UInteger
Success = 0
'Success
AccessDenied = 2
'Access denied
UnknownFailure = 8
'Unknown failure
InvalidName = 9
'Invalid name
InvalidLevel = 10
'Invalid level
InvalidParameter = 21
'Invalid parameter
DuplicateShare = 22
'Duplicate share
RedirectedPath = 23
'Redirected path
UnknownDevice = 24
'Unknown device or directory
NetNameNotFound = 25
'Net name not found
End Enum
Public Enum ShareType As UInteger
DiskDrive = &H0
'Disk Drive
PrintQueue = &H1
'Print Queue
Device = &H2
'Device
IPC = &H3
'IPC
DiskDriveAdmin = &H80000000UI
'Disk Drive Admin
PrintQueueAdmin = &H80000001UI
'Print Queue Admin
DeviceAdmin = &H80000002UI
'Device Admin
IpcAdmin = &H80000003UI
'IPC Admin
End Enum
Public mWinShareObject As ManagementObject
Public Sub New(obj As ManagementObject)
mWinShareObject = obj
End Sub
#Region "Wrap Win32_Share properties"
Public ReadOnly Property AccessMask() As UInteger
Get
Return Convert.ToUInt32(mWinShareObject("AccessMask"))
End Get
End Property
Public ReadOnly Property AllowMaximum() As Boolean
Get
Return Convert.ToBoolean(mWinShareObject("AllowMaximum"))
End Get
End Property
Public ReadOnly Property Caption() As String
Get
Return Convert.ToString(mWinShareObject("Caption"))
End Get
End Property
Public ReadOnly Property Description() As String
Get
Return Convert.ToString(mWinShareObject("Description"))
End Get
End Property
Public ReadOnly Property InstallDate() As DateTime
Get
Return Convert.ToDateTime(mWinShareObject("InstallDate"))
End Get
End Property
Public ReadOnly Property MaximumAllowed() As UInteger
Get
Return Convert.ToUInt32(mWinShareObject("MaximumAllowed"))
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return Convert.ToString(mWinShareObject("Name"))
End Get
End Property
Public ReadOnly Property Path() As String
Get
Return Convert.ToString(mWinShareObject("Path"))
End Get
End Property
Public ReadOnly Property Status() As String
Get
Return Convert.ToString(mWinShareObject("Status"))
End Get
End Property
Public ReadOnly Property Type() As ShareType
Get
Return CType(Convert.ToUInt32(mWinShareObject("Type")), ShareType)
End Get
End Property
#End Region
#Region "Wrap Methods"
Public Function Delete() As MethodStatus
Dim result As Object = mWinShareObject.InvokeMethod("Delete", New Object() {})
Dim r As UInteger = Convert.ToUInt32(result)
Return CType(r, MethodStatus)
End Function
Public Shared Function Create(path As String, name As String, type As ShareType, maximumAllowed As UInteger, description As String, password As String) As MethodStatus
Dim mc As New ManagementClass("Win32_Share")
Dim parameters As Object() = New Object() {path, name, CUInt(type), maximumAllowed, description, password, _
Nothing}
Dim result As Object = mc.InvokeMethod("Create", parameters)
Dim r As UInteger = Convert.ToUInt32(result)
Return CType(r, MethodStatus)
End Function
' TODO: Implement here GetAccessMask and SetShareInfo similarly to the above
#End Region
Public Shared Function GetAllShares() As IList(Of Win32Share)
Dim result As IList(Of Win32Share) = New List(Of Win32Share)()
Dim mc As New ManagementClass("Win32_Share")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo As ManagementObject In moc
Dim share As New Win32Share(mo)
result.Add(share)
Next
Return result
End Function
Public Shared Function GetNamedShare(name As String) As Boolean
' Not a very efficient implementation obviously, but heck... This is sample code. ;)
Dim shares As IList(Of Win32Share) = GetAllShares()
Dim count As Int32 = 0
For Each s As Win32Share In shares
If s.Name = name Then
Return True
End If
Next
' Return f
End Function
End Class
Now take a button in your windows application and write the following code :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If Win32Share.GetNamedShare("DeedNo") Then
MessageBox.Show("Folder Already Shared ")
Else
Dim folderNmae As String = Directory.CreateDirectory("D:\DeedNo").FullName
Dim result As Win32Share.MethodStatus = Win32Share.Create(folderNmae, "DeedNo", Win32Share.ShareType.DiskDrive, 5, "Shared Folder Creation", Nothing)
If result = Win32Share.MethodStatus.Success Then
MessageBox.Show("Shared folder successfully!")
Else
MessageBox.Show("Unable to share directory.")
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Now add a app.manifest file with the following code to run the .exe run as administrator:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>