Is File readable or writeable : File Command « File Stream « C# / C Sharp






Is File readable or writeable

     

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.AccessControl;
using System.IO;
using System.Security.Principal;

namespace Td.Additional.IO
{
    /// <summary>
    /// File facilities.
    /// </summary>
    public static class File
    {
        /// <summary>
        /// Determines whether the specified file is readable.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns>
        ///   <c>true</c> if the specified file is readable; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsReadable(string filename)
        {
            WindowsIdentity principal = WindowsIdentity.GetCurrent();
            if (System.IO.File.Exists(filename))
            {
                FileInfo fi = new FileInfo(filename);
                AuthorizationRuleCollection acl =
                    fi.GetAccessControl().GetAccessRules(true, true, typeof(SecurityIdentifier));
                for (int i = 0; i < acl.Count; i++)
                {
                    System.Security.AccessControl.FileSystemAccessRule rule = (System.Security.AccessControl.FileSystemAccessRule)acl[i];
                    if (principal.User.Equals(rule.IdentityReference))
                    {
                        if (System.Security.AccessControl.AccessControlType.Deny.Equals
                        (rule.AccessControlType))
                        {
                            if ((((int)FileSystemRights.Read) & (int)rule.FileSystemRights) == (int)(FileSystemRights.Read))
                                return false;
                        }
                        else if (System.Security.AccessControl.AccessControlType.Allow.Equals
                        (rule.AccessControlType))
                        {
                            if ((((int)FileSystemRights.Read) & (int)rule.FileSystemRights) == (int)(FileSystemRights.Read))
                                return true;
                        }
                    }
                }
            }
            else
            {
                return false;
            }
            return false;
        }

        /// <summary>
        /// Determines whether the specified file is writeable.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns>
        ///   <c>true</c> if the specified file is writeable; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsWriteable(string filename)
        {
            WindowsIdentity principal = WindowsIdentity.GetCurrent();
            if (System.IO.File.Exists(filename))
            {
                FileInfo fi = new FileInfo(filename);
                if (fi.IsReadOnly)
                    return false;
                AuthorizationRuleCollection acl =
                    fi.GetAccessControl().GetAccessRules(true, true, typeof(SecurityIdentifier));
                for (int i = 0; i < acl.Count; i++)
                {
                    System.Security.AccessControl.FileSystemAccessRule rule = (System.Security.AccessControl.FileSystemAccessRule)acl[i];
                    if (principal.User.Equals(rule.IdentityReference))
                    {
                        if (System.Security.AccessControl.AccessControlType.Deny.Equals
                        (rule.AccessControlType))
                        {
                            if ((((int)FileSystemRights.Write) & (int)rule.FileSystemRights) == (int)(FileSystemRights.Write))
                                return false;
                        }
                        else if (System.Security.AccessControl.AccessControlType.Allow.Equals
                        (rule.AccessControlType))
                        {
                            if ((((int)FileSystemRights.Write) & (int)rule.FileSystemRights) == (int)(FileSystemRights.Write))
                                return true;
                        }
                    }
                }
            }
            else
            {
                return false;
            }
            return false;
        }

    }
}

   
    
    
    
    
  








Related examples in the same category

1.C# Implementation of the File Copy Command
2.Use the FileInfo Class to Delete Files with Ease
3.File Move Implementation
4.Rename File Name
5.Rename File Extension
6.Backup File
7.Copy directory
8.Find string in a file
9.Converts a number of bytes to a shorter, more readable string representation
10.Compares the specified file1. This method accepts two strings the represent two files to compare.
11.File Renamer
12.Format File Size
13.Is File Existed
14.Replace in file
15.Send a file to the recycle bin
16.Copy file and folder
17.Gets last write time for a file.
18.Execute File
19.Gets the file size text.
20.Get File Bytes And Readable Size Information
21.Copy folder
22.Empty a Folder