Visual Studio .NET - Check File or Folder Permission

Asked By priyanka s on 26-May-09 02:06 AM

Hi All,

I have a list of users with me and I need to find out programmatically which of the users present in the list have read/write permissions on a particular folder/file. Is there a way to do it in vb.net ?

Thanks,

Priyanka

Santhosh N replied to priyanka s on 26-May-09 02:15 AM

You could check here how to get folder permissions

http://www.codeproject.com/KB/asp/useraccesscheck.aspx

need vb.net code - priyanka s replied to Santhosh N on 26-May-09 02:22 AM

thanks San

But is there a way to do the same in vb.net ?

Regards,

Priyanka

Santhosh N replied to priyanka s on 26-May-09 02:32 AM

Its a COM component and could be used in any .net language as far as I know...

Could you just try this and let me know

HI - Ravenet Rasaiyah replied to priyanka s on 26-May-09 03:41 AM
Hi

In the .NET Framework have a features to retrieve file permission list using "Access control List "

For example say you need a take(Test) folder permission list.


C# :
using System.Security.AccessControl;
using System.Security.Principal;


private string GetAccessControlInformation(string filename)
{
StringBuilder info = new StringBuilder();
info.AppendLine("ACE entries for the file \"" + filename + "\":");
info.AppendLine();
FileSecurity security = File.GetAccessControl(filename);
AuthorizationRuleCollection acl = security.GetAccessRules(true, true,
typeof(System.Security.Principal.NTAccount));
foreach (FileSystemAccessRule ace in acl)
{
string aceInfo = GetAceInformation(ace);
info.AppendLine(aceInfo);
}
return info.ToString();
}

private string GetAceInformation(FileSystemAccessRule ace)
{
StringBuilder info = new StringBuilder();
string line = string.Format("Account: {0}", ace.IdentityReference.Value);
info.AppendLine(line);
line = string.Format("Type: {0}", ace.AccessControlType);
info.AppendLine(line);
line = string.Format("Rights: {0}", ace.FileSystemRights);
info.AppendLine(line);
line = string.Format("Inherited ACE: {0}", ace.IsInherited);
info.AppendLine(line);
return info.ToString();
}


VB.NET

Private Function GetAccessControlInformation(ByVal filename As String) As String
Dim info As New StringBuilder()
info.AppendLine("ACE entries for the file """ & filename & """:")
info.AppendLine()
Dim security As FileSecurity = File.GetAccessControl(filename)
Dim acl As AuthorizationRuleCollection = security.GetAccessRules(True, True, GetType(System.Security.Principal.NTAccount))
For Each ace As FileSystemAccessRule In acl
Dim aceInfo As String = GetAceInformation(ace)
info.AppendLine(aceInfo)
Next
Return info.ToString()
End Function

Private Function GetAceInformation(ByVal ace As FileSystemAccessRule) As String
Dim info As New StringBuilder()
Dim line As String = String.Format("Account: {0}", ace.IdentityReference.Value)
info.AppendLine(line)
line = String.Format("Type: {0}", ace.AccessControlType)
info.AppendLine(line)
line = String.Format("Rights: {0}", ace.FileSystemRights)
info.AppendLine(line)
line = String.Format("Inherited ACE: {0}", ace.IsInherited)
info.AppendLine(line)
Return info.ToString()
End Function


Use:

Call this function GetAccessControlInformation(yourfolderPath), then it's return the all information.

if you have any question,post again.

thank you
priyanka s replied to Santhosh N on 26-May-09 05:47 AM

Hi San,

As i m naive to both .NET and COM techonology , can u give me some pointers as to how to go about it ?

Thanks a lot


priyanka s replied to Ravenet Rasaiyah on 26-May-09 05:50 AM

Hi Bill,

Thanks a lot for the code, but it only gives me the file permission list. Is there a way to find out if a particular user has access to that file?

Thanks

Re - Ravenet Rasaiyah replied to priyanka s on 26-May-09 10:03 AM
Hi

You can get list of users also, then iterate through the loop and check a user exist in the list. then you can get.

thank you
eric replied to priyanka s on 01-Aug-11 06:10 AM
Hi,

May i asked assuming that i have several folder  called "Agreement1 , 2, 3 etc." set up by the window administrator. Within this folder only certain lists of users will have access to agreement1 and 2, only a higher authority will have all 3 agreement folders. Possible to show the codes on how to do it?

Thank you

Regards
Eric