Open In App

File.GetAttributes() Method in C# with Examples

File.GetAttributes(String) is an inbuilt File class method that is used to get the file attributes of the file on the path. File attributes are those certain rights that are either granted or denied. These rights are for a user or for an operating system that accesses the file. These attributes are such as Read-only, Archive, System, Hidden etc.
 

Syntax:  



public static System.IO.FileAttributes GetAttributes (string path);

Parameter: This function accepts a parameter which is illustrated below: 

path: This is the specified file path.

Exceptions:
 



Returns: It returns the FileAttributes of the file on the path.
Below are the programs to illustrate the File.GetAttributes(String) method.
Program 1: Before running the below code, a file file.txt is created with some contents shown below-
 

 




// C# program to illustrate the usage
// of File.GetAttributes(String) method
 
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
 
class GFG {
    public static void Main()
    {
        // Specifying a file
        string path = @"file.txt";
 
        // Getting the file attributes
        FileAttributes attributes = File.GetAttributes(path);
 
        // Checking if the file is having whether hidden attributes
 
        // If the file is having hidden attribute then
        // that attribute will be removed
        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
            // Removing the file's hidden attribute
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
 
            // Calling the SetAttributes() function
            File.SetAttributes(path, attributes);
            Console.WriteLine("The {0} file is no longer hidden.", path);
        }
        else {
            // Calling the SetAttributes() function to
            // set hidden attribute
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
            Console.WriteLine("The {0} file is now hidden.", path);
        }
    }
 
    private static FileAttributes RemoveAttribute(FileAttributes attributes,
                                          FileAttributes attributesToRemove)
    {
        return attributes & ~attributesToRemove;
    }
}

Output:  

The file.txt file is now hidden.

Program 2: Initially, no file was created. Below code, itself creates a file gfg.txt.
 




// C# program to illustrate the usage
// of File.GetAttributes(String) method
 
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
 
class GFG {
    public static void Main()
    {
        // Specifying a file
        string path = @"gfg.txt";
 
        // Create the file if it does not exist.
        if (!File.Exists(path)) {
            File.Create(path);
        }
 
        // Getting the file attributes
        FileAttributes attributes = File.GetAttributes(path);
 
        // Checking if the file is having whether hidden attributes
 
        // If the file is having hidden attribute then
        // that attribute will be removed
        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
            // Removing the file's hidden attribute
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
 
            // Calling the SetAttributes() function
            File.SetAttributes(path, attributes);
            Console.WriteLine("The {0} file is no longer hidden.", path);
        }
        else {
            // Calling the SetAttributes() function to
            // set hidden attribute
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
            Console.WriteLine("The {0} file is now hidden.", path);
        }
    }
 
    private static FileAttributes RemoveAttribute(FileAttributes attributes,
                                          FileAttributes attributesToRemove)
    {
        return attributes & ~attributesToRemove;
    }
}

Output:  

The gfg.txt file is now hidden.

 


Article Tags :
C#