Do we have any other code monkies in here?

H
hudsmack

Every once in a while I thought I might post some code tips.  The following code will check for read ACLs on a file in C#.  We had a situation come up at work where one of the developers was having a problem reading one of our log files with user permissions.  It turns out he was attempting to open the file using a full control access flag, but I thought this little piece of code could come in handy.


If you have any questions about the code, feel free to post in the comments and I will do my best to answer all the questions.  If there are any other code bits you want to see, let me know and I'll put some write-ups together.


using System;


using System.IO;


using System.Security;


using System.Security.Principal;


using System.Security.AccessControl;


using System.Collections.Generic;


using System.Text;


 


namespace TestReader


{


    class Program


    {


        static void Main(string[] args)


        {


            string myPath = @"C:temptest.txt";


            string[] fileRead;


            try


            {


                if (File.Exists(myPath))


                {


                    if (CanRead(myPath))


                    {


                        File.OpenRead(myPath);


                        fileRead = File.ReadAllLines(myPath);


                        foreach (string line in fileRead)


                        {


                            Console.WriteLine(line);


                        }


                    }


                    else


                    {


                        Console.WriteLine("No permissions to read the file.");


                    }


                }


            }


            catch (Exception ex)


            {


                Console.WriteLine(ex.Message);


            }


 


        }


 


        private static bool CanRead(string fname)


        {


            try


            {


                WindowsIdentity WinIdentity = WindowsIdentity.GetCurrent();


                WindowsPrincipal WinPrincipal = new WindowsPrincipal(WinIdentity);


                bool AllowRead = false;


                FileInfo fi = new FileInfo(fname);


                FileSecurity sec = fi.GetAccessControl();


                foreach (FileSystemAccessRule rule in sec.GetAccessRules(true, true, typeof(NTAccount)))


                {


                    if (WinPrincipal.IsInRole(rule.IdentityReference.Value))


                    {


                        if (((int)rule.FileSystemRights & (int)FileSystemRights.Read) > 0)


                        {


                            if (rule.AccessControlType == AccessControlType.Allow)


                            {


                                AllowRead = true;


                            }


                            else if (rule.AccessControlType == AccessControlType.Deny)


                            {


                                return false;


                            }


                        }


                    }


                }


                return AllowRead;


            }


            catch (Exception ex)


            {


                Console.WriteLine(ex.Message);


                return false;


            }


        }


    }


}

Comments (0)

No comments yet. Be the first to share your thoughts!