Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
communitycreator

What is File.GetAttributes() in C#?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

We can use the GetAttributes() method of the File class to get the attributes of a file. These attributes refer to the permissions or rights associated with the file. For instance, there are some files that can only be accessed or modified by an admin but not by normal users. Some attributes are read-only, hidden, and normal.

Syntax

File.GetAttributes(fileName)

Parameters

fileName: This is the name of the file.

Return value

The FileAttributes of the file is returned.

Example

main.cs
test.txt
// using some namespaces
using System;
using System.IO;
using System.Security.AccessControl;
class HelloWorld
{
static void Main()
{
// get file name
string file = "test.txt";
// get attributes of the file
Console.WriteLine(File.GetAttributes(file));
}
}
Get the attributes of a file in C#

Explanation

  • The file test.txt was created. We want to get the attributes of that file.
  • Line 1: We get the name of the file of which we want the attributes.
  • Line 13: We get the attributes of the file using the GetAttributes() method. We print its value to the console.

Note: The result above is normal. A file is normal if it is not special in some way. Normal files are only used to contain data. Unlike a normal file, special files include symbolic links and directories.

RELATED TAGS

c#
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring