C# - How to Get Attributes of a Specified File?

Learn, how to get attributes of a file using C# program?
Submitted by IncludeHelp, on October 29, 2017 [Last updated : March 26, 2023]

Given a file, and we have to get and print file's attribute using C# program.

To get attributes of a specified file in C#, we use File.GetAttributes() method.

File.GetAttributes()

This is a method of "File" class, which returns the specified file's attributes.

Syntax

FileAttributes GetAttributes(path);

Parameter(s)

  1. path - Filename with its location.

Return Value

This method returns object of FileAttributes, File attribute can be following:

  • Archive
  • Compressed
  • Device
  • Hidden
  • ReadOnly, etc

C# program to get attributes of a specified file

using System;
using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      FileAttributes F = File.GetAttributes("B123.TXT");

      Console.WriteLine("Attributes are :" + F.ToString());
    }
  }
}

Output

Attributes are :Hidden, Archive

Explanation

In the above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program.

C# File Handling Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.