Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

How to change access permitions for directory?

Can I grant some access permitions for some folder (or shared folder) to some user (or group) programmatically?

Mikhail Andronov
Monday, December 9, 2002

You can, but it's really ugly. You need to use the WMI classes for this. Here's a tiny snippet that does no more than copy the DACL from one file to another:

    Private Sub btnCopyDACL_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCopyDACL.Click
        Dim lfs As ROOT.CIMV2.Win32.LogicalFileSecuritySetting = _
        New ROOT.CIMV2.Win32.LogicalFileSecuritySetting("c:\temp\Test.txt")
        Dim mbo As Management.ManagementBaseObject = Nothing
        Dim i As UInt32 = lfs.GetSecurityDescriptor(mbo)
        Dim fs As FileStream = _
        New FileStream("c:\temp\Test2.txt", FileMode.Create)
        Dim sw As StreamWriter = New StreamWriter(fs)
        sw.Write("This is some sample text")
        Dim lfs2 As ROOT.CIMV2.Win32.LogicalFileSecuritySetting = _
        New ROOT.CIMV2.Win32.LogicalFileSecuritySetting("c:\temp\Test2.txt")
        Dim j As UInt32 = lfs2.SetSecurityDescriptor(mbo)
    End Sub

The applicable WMI classes aren't documented especially well, either.

Mike Gunderloy
Monday, December 9, 2002

*  Recent Topics

*  Fog Creek Home