C# Program to get information about a file

To get information about a file means to retrieve the attributes and properties associated with that particular file. File attributes indicate characteristics such as whether a file is normal, hidden, archived, read-only, or a system file.

In C#, the FileInfo class provides comprehensive information about files, including attributes, size, creation time, and modification time.

Syntax

Following is the syntax for creating a FileInfo object and getting file attributes −

FileInfo info = new FileInfo("filename.txt");
FileAttributes attr = info.Attributes;

Using FileInfo to Get File Attributes

The FileAttributes enumeration represents various file attributes that can be set on a file. Common attributes include Normal, Hidden, ReadOnly, Archive, and System.

Example

using System.IO;
using System;

public class Program {
    public static void Main() {
        using (StreamWriter sw = new StreamWriter("hello.txt")) {
            sw.WriteLine("This is demo text!");
        }
        
        FileInfo info = new FileInfo("hello.txt");
        FileAttributes attr = info.Attributes;
        Console.WriteLine("File attributes: " + attr);
    }
}

The output of the above code is −

File attributes: Archive

Getting Comprehensive File Information

The FileInfo class provides many properties beyond just attributes. You can retrieve creation time, last write time, file size, and more.

Example

using System.IO;
using System;

public class Program {
    public static void Main() {
        using (StreamWriter sw = new StreamWriter("sample.txt")) {
            sw.WriteLine("Sample file content for testing.");
        }
        
        FileInfo info = new FileInfo("sample.txt");
        
        Console.WriteLine("File Name: " + info.Name);
        Console.WriteLine("Full Path: " + info.FullName);
        Console.WriteLine("File Size: " + info.Length + " bytes");
        Console.WriteLine("Creation Time: " + info.CreationTime);
        Console.WriteLine("Last Write Time: " + info.LastWriteTime);
        Console.WriteLine("Attributes: " + info.Attributes);
        Console.WriteLine("Is Read Only: " + info.IsReadOnly);
    }
}

The output of the above code is −

File Name: sample.txt
Full Path: C:\path\to\sample.txt
File Size: 34 bytes
Creation Time: 12/15/2023 10:30:15 AM
Last Write Time: 12/15/2023 10:30:15 AM
Attributes: Archive
Is Read Only: False

Checking Specific File Attributes

You can check for specific attributes using bitwise operations with the FileAttributes enumeration.

Example

using System.IO;
using System;

public class Program {
    public static void Main() {
        using (StreamWriter sw = new StreamWriter("test.txt")) {
            sw.WriteLine("Test file for attribute checking.");
        }
        
        FileInfo info = new FileInfo("test.txt");
        FileAttributes attr = info.Attributes;
        
        Console.WriteLine("File attributes: " + attr);
        
        if ((attr & FileAttributes.Hidden) == FileAttributes.Hidden) {
            Console.WriteLine("File is hidden");
        } else {
            Console.WriteLine("File is not hidden");
        }
        
        if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) {
            Console.WriteLine("File is read-only");
        } else {
            Console.WriteLine("File is not read-only");
        }
        
        if ((attr & FileAttributes.Archive) == FileAttributes.Archive) {
            Console.WriteLine("File has archive attribute");
        } else {
            Console.WriteLine("File does not have archive attribute");
        }
    }
}

The output of the above code is −

File attributes: Archive
File is not hidden
File is not read-only
File has archive attribute

Common File Attributes

Attribute Description
Normal File has no special attributes
Archive File is marked for backup or archival
Hidden File is hidden from normal directory listings
ReadOnly File cannot be modified or deleted
System File is a system file

Conclusion

The FileInfo class in C# provides comprehensive access to file information including attributes, size, timestamps, and more. Use the FileAttributes enumeration to check specific file characteristics, and leverage bitwise operations to test for multiple attributes efficiently.

Updated on: 2026-03-17T07:04:35+05:30

267 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements