none
Check User read access to a file share RRS feed

  • Question

  • Hi,

    I want to check if the logged in user have the access to a file share before he launches the application present in that file share.

    public static string userName = System.Environment.UserName;

    Then need to check if the userName have read access to share path -->

    \\opfilesr2\aquarius

    Can you please help me with the code logic? 

    Monday, February 11, 2019 6:50 AM

All replies

  • Hi Pallavi Ganesh,

    Thank you for posting here.

    For your question, you could try the code below to get the users of specified directory permissions except Administrators and SYSTEM. All the path would be okay not only for shared files.

     class Program
        {
    
            static void Main(string[] args)
            {
                GetDirectoryAccountSecurity(@"C:\Users\Desktop\New");
                Console.ReadKey();
            }
            public static List<Account> GetDirectoryAccountSecurity(string DirName)
            {
                List<Account> account = new List<Account>();
                DirectoryInfo dInfo = new DirectoryInfo(DirName);
                if (dInfo.Exists)
                {
                    DirectorySecurity sec = Directory.GetAccessControl(DirName, AccessControlSections.All);
                    foreach (FileSystemAccessRule rule in sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
                    {
                        if (rule.IdentityReference.Value != @"NT AUTHORITY\SYSTEM" && rule.IdentityReference.Value != @"BUILTIN\Administrators")
                        {
                            Account acc = new Account()
                            {
                                UserName = rule.IdentityReference.Value,
                                Permission = rule.FileSystemRights.ToString()
                            };
                            account.Add(acc);
                        }
    
                    }
                }
                return account;
            }
    
        }
        public class Account
        {
            public string UserName { get; set; }
            public string Permission { get; set; }
        }

    My shared folder

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, February 12, 2019 2:55 AM
    Moderator