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.
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.
- 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:
The file.txt file is now hidden.
Program 2: Initially, no file was created. Below code, itself creates a file gfg.txt.
The gfg.txt file is now hidden.
- Uri.HexUnescape() Method in C# with Examples
- Uri.GetLeftPart() Method in C# with Examples
- MathF.Exp() Method in C# with Examples
- MathF.Log() Method in C# with Examples
- MathF.Pow() Method in C# with Examples
- MathF.Max() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- Uri.ReferenceEquals() Method in C# with Examples
- MathF.Min() Method in C# with Examples
- MathF.Cos() Method in C# with Examples
- MathF.Tan() Method in C# with Examples
- MathF.Abs() Method in C# with Examples
- SByte.GetHashCode Method in C# with Examples
- Double.CompareTo Method in C# with Examples
- SByte.Equals Method in C# with Examples
- SByte.GetTypeCode Method in C# with Examples
- SByte.ToString() Method in C# with Examples | Set - 1
- SByte.ToString Method in C# with Examples | Set - 2
- UInt32.GetHashCode Method in C# with Examples
- UInt32.Equals Method in C# with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.