File.GetAttributes() Method in C# with Examples
Last Updated :
26 Feb, 2021
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:
- ArgumentException: The path is empty, contains only white spaces, or invalid characters.
- PathTooLongException: The specified path, file name, or both exceed the system-defined maximum length.
- NotSupportedException: The path is in an invalid format.
- FileNotFoundException: The path represents a file and is invalid, such as being on an unmapped drive, or the file cannot be found.
- DirectoryNotFoundException: The path represents a directory and is invalid, such as being on an unmapped drive or the directory cannot be found.
- IOException: This file is being used by another process.
- UnauthorizedAccessException: The caller does not have the required permission.
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-
CSharp
using System;
using System.IO;
using System.Text;
class GFG {
public static void Main()
{
string path = @"file.txt" ;
FileAttributes attributes = File.GetAttributes(path);
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
File.SetAttributes(path, attributes);
Console.WriteLine( "The {0} file is no longer hidden." , path);
}
else {
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.
CSharp
using System;
using System.IO;
using System.Text;
class GFG {
public static void Main()
{
string path = @"gfg.txt" ;
if (!File.Exists(path)) {
File.Create(path);
}
FileAttributes attributes = File.GetAttributes(path);
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
File.SetAttributes(path, attributes);
Console.WriteLine( "The {0} file is no longer hidden." , path);
}
else {
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.
Share your thoughts in the comments
Please Login to comment...