File.SetAttributes() Method in C# with Examples

File.SetAttributes(String, FileAttributes) is an inbuilt File class method that is used to set the specified file attributes of the file on the specified 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 void SetAttributes (string path, System.IO.FileAttributes fileAttributes);

Parameter: This function accepts two parameters which are illustrated below:

  • path: This is the specified file path.
  • fileAttributes: This is the bitwise combination of the enumeration values.

Exceptions:

  • ArgumentException: The path is empty, contains only white spaces, invalid characters, or the file attribute is invalid.
  • PathTooLongException: The specified path, file name, or both exceed the system-defined maximum length.
  • NotSupportedException: The path is in an invalid format.
  • DirectoryNotFoundException: The specified path is invalid.
  • FileNotFoundException: The file cannot be found.
  • UnauthorizedAccessException: The path specified a file that is read-only. OR this operation is not supported on the current platform. OR the path specified a directory. OR the caller does not have the required permission.

Below are the programs to illustrate the File.SetAttributes(String, FileAttributes) method.



Program 1: Before running the below code, a file file.txt is created with some contents shown below:

file.txt

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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()
    {
        // Specifing 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;
    }
}

chevron_right


Output:

The file.txt file is now hidden.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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()
    {
        // Specifing 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;
    }
}

chevron_right


Output:

The gfg.txt file is now hidden.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.