Solved

How can I change the permissions of a folder on a remote server

Posted on 2003-10-23
5
194 Views
Last Modified: 2010-05-01
Hello,

I have to change the permissions of a folder on a remote server. How can I do this in VB.Net.

Greets
Sascha
0
Comment
Question by:srexp
5 Comments
 
LVL 1

Accepted Solution

by:
s_sarigul earned 125 total points
ID: 9605651
'************************************************
'******I THINK IT WORKS IN ONLY NT/2000/XP*************
'************************************************
Option Explicit
Private Const NERR_SUCCESS As Long = 0&

'share types
Private Const STYPE_ALL As Long = -1 'note: my const
Private Const STYPE_DISKTREE As Long = 0
Private Const STYPE_PRINTQ As Long = 1
Private Const STYPE_DEVICE As Long = 2
Private Const STYPE_IPC As Long = 3
Private Const STYPE_SPECIAL As Long = &H80000000

'permissions
Private Const ACCESS_READ As Long = &H1
Private Const ACCESS_WRITE As Long = &H2
Private Const ACCESS_CREATE As Long = &H4
Private Const ACCESS_EXEC As Long = &H8
Private Const ACCESS_DELETE As Long = &H10
Private Const ACCESS_ATRIB As Long = &H20
Private Const ACCESS_PERM As Long = &H40
Private Const ACCESS_ALL As Long = ACCESS_READ Or _
ACCESS_WRITE Or _
ACCESS_CREATE Or _
ACCESS_EXEC Or _
ACCESS_DELETE Or _
ACCESS_ATRIB Or _
ACCESS_PERM

Private Type SHARE_INFO_2
shi2_netname As Long
shi2_type As Long
shi2_remark As Long
shi2_permissions As Long
shi2_max_uses As Long
shi2_current_uses As Long
shi2_path As Long
shi2_passwd As Long
End Type

Private Declare Function NetShareAdd Lib "netapi32" _
(ByVal servername As Long, _
ByVal level As Long, _
buf As Any, _
parmerr As Long) As Long


Private Sub Form_Load()
'Server Name
Text1.Text = "\\" & Environ$("COMPUTERNAME")
'Path
Text2.Text = "c:\program files"
'Display Name
Text3.Text = "ProgramFiles"
'Description
Text4.Text = "VB API Share Test"
'Password
Text5.Text = ""

End Sub


Private Sub Command1_Click()

Dim success As Long

success = ShareAdd(Text1.Text, _
Text2.Text, _
Text3.Text, _
Text4.Text, _
Text5.Text)

Select Case success
Case 0: MsgBox "share created successfully!"
Case 2118: MsgBox "share name already exists"
Case Else: MsgBox "create error " & success
End Select

End Sub


Private Function ShareAdd(sServer As String, _
sSharePath As String, _
sShareName As String, _
sShareRemark As String, _
sSharePw As String) As Long

Dim dwServer As Long
Dim dwNetname As Long
Dim dwPath As Long
Dim dwRemark As Long
Dim dwPw As Long
Dim parmerr As Long
Dim si2 As SHARE_INFO_2

dwServer = StrPtr(sServer)
dwNetname = StrPtr(sShareName)
dwPath = StrPtr(sSharePath)

If Len(sShareRemark) > 0 Then
dwRemark = StrPtr(sShareRemark)
End If

If Len(sSharePw) > 0 Then
dwPw = StrPtr(sSharePw)
End If

With si2
.shi2_netname = dwNetname
.shi2_path = dwPath
.shi2_remark = dwRemark
.shi2_type = STYPE_DISKTREE
.shi2_permissions = ACCESS_ALL
.shi2_max_uses = -1
.shi2_passwd = dwPw
End With

ShareAdd = NetShareAdd(dwServer, _
2, _
si2, _
parmerr)

End Function

'*******************************************************
'THIS IS ANOTHER ONE
'*******************************************************
'=====General Declaration======
Private Type Share_Info_50
    shi50_netname As String * 13
    shi50_type As Byte
    shi50_flags As Integer
    shi50_remark As String
    shi50_path As String
    shi50_rw_pssword As String * 9
    shi50_ro_password As String * 9
End Type

Public Const SHI50F_RDONLY = &H1
Public Const SHI50F_FULL = &H2
Public Const SHI50F_DEPENDSON = SHI50F_RDONLY Or SHI50F_FULL
Public Const SHI50F_ACCESSMASK = SHI50F_RDONLY Or SHI50F_FULL
Public Const SHI50F_PERSIST = &H100
Public Const SHI50F_SYSTEM = &H200
Public Const STYPE_DISKTREE = 0
Public Const SHI_USES_UNLIMITED = 31

Private Declare Function NetShareAdd Lib "Svrapi.dll" _
    (ByVal Server As String, _
     ByVal Level As Long, _
     si As Share_Info_50, _
     ByVal Length As Long) As Long

'======Function to share directory========
Public Function Share(ByVal lpShareName As String, _
                      ByVal lpPath As String, _
                      ByVal lpRemark As String, _
                      ByVal lpPassword As String) As Long
                                   
   Dim lRtn As Long
   Dim lLen As Long
   Dim si50 As Share_Info_50
   
   si50.shi50_netname = lpShareName + vbNullChar
   si50.shi50_type = STYPE_DISKTREE
   si50.shi50_remark = lpRemark + vbNullChar
   si50.shi50_path = UCase(lpPath) + vbNullChar
   si50.shi50_ro_password = lpPassword + vbNullChar
   si50.shi50_rw_pssword = lpPassword + vbNullChar
   si50.shi50_flags = SHI50F_PERSIST Or SHI50F_FULL
   
   lLen = Len(si50)
   
   lRtn = NetShareAdd(vbNullString, 50, si50, lLen)
End Function


0
 

Author Comment

by:srexp
ID: 9606246
I think this is an VB6 example. What I need is VB.Net. Can you please translate it to VB.net. It is hard for me to do that, because I have not enough skills in VB6. Thanks.
0
 
LVL 27

Assisted Solution

by:planocz
planocz earned 125 total points
ID: 10164960
Hi srexp,

Here is a translation of the above code....
paste into a new .net form........

Option Explicit On
Imports System.Reflection
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private Const NERR_SUCCESS As Long = 0&

    'share types
    Private Const STYPE_ALL As Long = -1 'note: my const
    Private Const STYPE_DISKTREE As Long = 0
    Private Const STYPE_PRINTQ As Long = 1
    Private Const STYPE_DEVICE As Long = 2
    Private Const STYPE_IPC As Long = 3
    Private Const STYPE_SPECIAL As Long = &H80000000

    'permissions
    Private Const ACCESS_READ As Long = &H1
    Private Const ACCESS_WRITE As Long = &H2
    Private Const ACCESS_CREATE As Long = &H4
    Private Const ACCESS_EXEC As Long = &H8
    Private Const ACCESS_DELETE As Long = &H10
    Private Const ACCESS_ATRIB As Long = &H20
    Private Const ACCESS_PERM As Long = &H40
    Private Const ACCESS_ALL As Long = ACCESS_READ Or _
    ACCESS_WRITE Or ACCESS_CREATE Or _
    ACCESS_EXEC Or ACCESS_DELETE Or _
    ACCESS_ATRIB Or ACCESS_PERM

    Private Structure SHARE_INFO_2
        Dim shi2_netname As Long
        Dim shi2_type As Long
        Dim shi2_remark As Long
        Dim shi2_permissions As Long
        Dim shi2_max_uses As Long
        Dim shi2_current_uses As Long
        Dim shi2_path As Long
        Dim shi2_passwd As Long
    End Structure

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.TextBox3 = New System.Windows.Forms.TextBox
        Me.TextBox4 = New System.Windows.Forms.TextBox
        Me.TextBox5 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(16, 24)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(104, 20)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(16, 64)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(104, 20)
        Me.TextBox2.TabIndex = 1
        Me.TextBox2.Text = "TextBox2"
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(16, 100)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(104, 20)
        Me.TextBox3.TabIndex = 2
        Me.TextBox3.Text = "TextBox3"
        '
        'TextBox4
        '
        Me.TextBox4.Location = New System.Drawing.Point(16, 132)
        Me.TextBox4.Name = "TextBox4"
        Me.TextBox4.Size = New System.Drawing.Size(104, 20)
        Me.TextBox4.TabIndex = 3
        Me.TextBox4.Text = "TextBox4"
        '
        'TextBox5
        '
        Me.TextBox5.Location = New System.Drawing.Point(16, 168)
        Me.TextBox5.Name = "TextBox5"
        Me.TextBox5.Size = New System.Drawing.Size(104, 20)
        Me.TextBox5.TabIndex = 4
        Me.TextBox5.Text = "TextBox5"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(152, 200)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 36)
        Me.Button1.TabIndex = 5
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox5)
        Me.Controls.Add(Me.TextBox4)
        Me.Controls.Add(Me.TextBox3)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Declare Ansi Function NetShareAdd Lib "netapi32" _
    (ByVal servername As Long, ByVal level As Long, _
    ByVal buf As String, ByVal parmerr As Long) As Long

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Server Name
        TextBox1.Text = "\\" & Environ$("COMPUTERNAME")
        'Path
        TextBox2.Text = "c:\program files"
        'Display Name
        TextBox3.Text = "ProgramFiles"
        'Description
        TextBox4.Text = "VB API Share Test"
        'Password
        TextBox5.Text = ""
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim success As Long

        success = ShareAdd(TextBox1.Text, TextBox2.Text, _
        TextBox3.Text, TextBox4.Text, TextBox5.Text)

        Select Case success
            Case 0 : MsgBox("share created successfully!")
            Case 2118 : MsgBox("share name already exists")
            Case Else : MsgBox("create error " & success)
        End Select
    End Sub


    Private Function ShareAdd(ByVal sServer As String, _
    ByVal sSharePath As String, ByVal sShareName As String, _
    ByVal sShareRemark As String, ByVal sSharePw As String) As Long

        Dim dwServer As Long
        Dim dwNetname As Long
        Dim dwPath As Long
        Dim dwRemark As Long
        Dim dwPw As Long
        Dim parmerr As Long
        Dim si2 As SHARE_INFO_2

        dwServer = CLng(sServer)
        dwNetname = CLng(sShareName)
        dwPath = CLng(sSharePath)

        If Len(sShareRemark) > 0 Then
            dwRemark = CLng(sShareRemark)
        End If

        If Len(sSharePw) > 0 Then
            dwPw = CLng(sSharePw)
        End If

        With si2
            .shi2_netname = dwNetname
            .shi2_path = dwPath
            .shi2_remark = dwRemark
            .shi2_type = STYPE_DISKTREE
            .shi2_permissions = ACCESS_ALL
            .shi2_max_uses = -1
            .shi2_passwd = dwPw
        End With

        ShareAdd = NetShareAdd(dwServer, 2, si2.ToString, parmerr)

    End Function
    ''*******************************************************
    'THIS IS ANOTHER ONE
    '*******************************************************
    '=====General Declaration======

    Private Structure Share_Info_50
        Dim shi50_netname As String  '13 length
        Dim shi50_type As Byte
        Dim shi50_flags As Integer
        Dim shi50_remark As String
        Dim shi50_path As String
        Dim shi50_rw_pssword As String  '9 length
        Dim shi50_ro_password As String '9 length
    End Structure

    Public Const SHI50F_RDONLY = &H1
    Public Const SHI50F_FULL = &H2
    Public Const SHI50F_DEPENDSON = SHI50F_RDONLY Or SHI50F_FULL
    Public Const SHI50F_ACCESSMASK = SHI50F_RDONLY Or SHI50F_FULL
    Public Const SHI50F_PERSIST = &H100
    Public Const SHI50F_SYSTEM = &H200
    Public Const STYPE_DISKTREE = 0
    Public Const SHI_USES_UNLIMITED = 31

    Private Declare Function NetShareAdd Lib "Svrapi.dll" _
        (ByVal Server As String, _
         ByVal Level As Long, _
    ByVal si As Share_Info_50, _
         ByVal Length As Long) As Long

    '======Function to share directory========
    Public Function Share(ByVal lpShareName As String, _
                          ByVal lpPath As String, _
                          ByVal lpRemark As String, _
                          ByVal lpPassword As String) As Long

        Dim lRtn As Long
        Dim lLen As Long
        Dim si50 As Share_Info_50

        si50.shi50_netname = lpShareName + vbNullChar
        si50.shi50_type = STYPE_DISKTREE
        si50.shi50_remark = lpRemark + vbNullChar
        si50.shi50_path = UCase(lpPath) + vbNullChar
        si50.shi50_ro_password = lpPassword + vbNullChar
        si50.shi50_rw_pssword = lpPassword + vbNullChar
        si50.shi50_flags = SHI50F_PERSIST Or SHI50F_FULL

        lLen = Len(si50)

        lRtn = NetShareAdd(vbNullString, 50, si50.ToString, lLen)
    End Function

End Class
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now