Getting Folder Permissions

  • Thread starter Thread starter Hari Shankar
  • Start date Start date
H

Hari Shankar

Hello folks

I have to get the NTFS permissions of a folder programmatically. I am using
C#. Pls hint me if i am missing anything in the code below.

private void GetSecurityPermission(string strShareName)

{

NERR eRetVal = NERR.ERROR_FAILURE;

EXPLICIT_ACCESS oEA;

IntPtr pZero = IntPtr.Zero; IntPtr pSidOwner = pZero; IntPtr pSidGroup =
pZero; IntPtr pSACL = pZero; IntPtr pDACL = pZero;IntPtr psd = pZero;

int nExplicitEntriesCount = 0;bool bDaclPresent;IntPtr ppDacl;bool
bDaclDefaulted;

///Get the DACL information of strShareName using GetNamedSecurityInfo()
API.

/// SE_FILE_OBJECT constant says that the named securable object is a
file or folder

eRetVal = CDecls.GetNamedSecurityInfo(strShareName,
SE_OBJECT_TYPE.SE_FILE_OBJECT,
SECURITY_INFORMATION.DACL_SECURITY_INFORMATION, out pSidOwner,
out pSidGroup, out pDACL, out pSACL, out psd);

if (eRetVal == NERR.ERROR_SUCCESS)

{

eRetVal = CDecls.GetExplicitEntriesFromAcl(pDACL, ref
nExplicitEntriesCount, out oEA);

if(eRetVal == NERR.ERROR_SUCCESS)

{

for (int nIndex = 0; nIndex < nExplicitEntriesCount; nIndex++)

{

}

}

}

}

Thanx a ton.

Ciao

Hari
 
Actually i am trying to get the security permissions of a share. I am
getting the return value as SUCCESS. But i am not getting the correct data
in oEA structure.

Hari
 
Hari Shankar said:
Actually i am trying to get the security permissions of a share. I am
getting the return value as SUCCESS. But i am not getting the correct data
in oEA structure.

Hari

It's very hard to see from the code snip you posted as you don't process the
oEA structure in your code, so I guess you run this from the debugger, but
you can start by correcting this...

SE_OBJECT_TYPE.SE_FILE_OBJECT,
Should be:
SE_OBJECT_TYPE.SE_LMSHARE

I would also suggest you use the System.DirectoryServices (and ADSI interop)
or System.Management (and WMI) to check ACL's on File or Share objects.
Another option is to use Managed C++, C# is not the right language for this
kind of job.

Willy.
 
Back
Top