File Attributes code samples in C#

In this article, learn how to read various file attributes from any directory or file using C# with code samples.


File Attributes code samples in C#

In C#, the FileAttributes enumeration is used to represent the attributes of a file, such as whether it is read-only, hidden, system, or archive. The FileAttributes enumeration is part of the System.IO namespace and provides a set of static members that can be used to set or retrieve the attributes of a file.

The following are the possible values of the FileAttributes enumeration:

Archive: indicates that the file is marked for backup or archiving purposes.
Compressed: indicates that the file is compressed.
Directory: indicates that the file is a directory.
Hidden: indicates that the file is hidden.
Normal: indicates that the file has no other attributes set.
ReadOnly: indicates that the file is read-only.
System: indicates that the file is a system file.
Temporary: indicates that the file is temporary.

Function to check whether path is FILE or DIRECTORY:

/// 
/// Function to check whether path is FILE or DIRECTORY 
/// 
/// 
/// 
public static bool IsDirectory(string sDirectoryOrFilePath)
   {
       try
       {
           FileAttributes attr = File.GetAttributes(sDirectoryOrFilePath);
                
           if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
           {
               //Its Directory
               return false;
           }

           //Its File
           return true;
       }
       catch (DirectoryNotFoundException ex)
       {
           throw ex;
       }
       catch (FileNotFoundException ex)
       {
           throw ex;
       }  
}

Function to check whether the FILE or DIRECTORY is hidden or not:

/// 
/// Function to check whether the FILE or DIRECTORY is hidden or not.
/// 
/// 
/// 
public static bool IsHiddenFile(string sDirectoryOrFilePath)
{
     try
     {
          FileAttributes attr = File.GetAttributes(sDirectoryOrFilePath);

          if ((attr & FileAttributes.Hidden) == FileAttributes.Hidden)
          {
               //Its Hidden file
               return true;
          }

          //Its Archive file.
          return false;
      }
      catch (DirectoryNotFoundException ex)
      {
          throw ex;
      }
      catch (FileNotFoundException ex)
      {
          throw ex;
      }
}

Function to check whether the FILE or DIRECTORY is hidden or not.

/// 
/// Function to check whether the FILE or DIRECTORY is readonly or not.
/// 
/// 
/// 
public static bool IsReadOnlyFile(string sDirectoryOrFilePath)
{
    try
    {
        FileAttributes attr = File.GetAttributes(sDirectoryOrFilePath);
                
        if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            //Its ReadOnly file
            return true;
        }

        //Its Archive file.
        return false;
    }
    catch (DirectoryNotFoundException ex)
    {
        throw ex;
    }
    catch (FileNotFoundException ex)
    {
        throw ex;
    }
}

– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:

Tags:

We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo