Check if a File is hidden in C#

To check if a file is hidden in C#, use the FileAttributes enumeration which contains various file attribute members like Compressed, Directory, Hidden, and others. The FileAttributes.Hidden flag indicates whether a file has the hidden attribute set.

You can retrieve file attributes using File.GetAttributes() and then check for the Hidden flag using bitwise operations.

Syntax

Following is the syntax for getting file attributes −

FileAttributes attributes = File.GetAttributes(filePath);

Following is the syntax for checking if a file is hidden −

bool isHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden;

Using File.GetAttributes() to Check Hidden Files

Example

using System;
using System.IO;

class Program {
   public static void Main() {
      string filePath = "test.txt";
      
      // Create a test file first
      File.WriteAllText(filePath, "This is a test file.");
      
      // Get file attributes
      FileAttributes attributes = File.GetAttributes(filePath);
      
      // Check if the file is hidden
      bool isHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
      
      Console.WriteLine("File: " + filePath);
      Console.WriteLine("Is Hidden: " + isHidden);
      Console.WriteLine("Attributes: " + attributes);
      
      // Clean up
      File.Delete(filePath);
   }
}

The output of the above code is −

File: test.txt
Is Hidden: False
Attributes: Archive

Setting and Checking Hidden Attribute

Example

using System;
using System.IO;

class Program {
   public static void Main() {
      string filePath = "hiddenfile.txt";
      
      // Create a test file
      File.WriteAllText(filePath, "This file will be hidden.");
      
      // Set the file as hidden
      File.SetAttributes(filePath, File.GetAttributes(filePath) | FileAttributes.Hidden);
      Console.WriteLine("File has been set as hidden.");
      
      // Check if the file is now hidden
      FileAttributes attributes = File.GetAttributes(filePath);
      bool isHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
      
      Console.WriteLine("File: " + filePath);
      Console.WriteLine("Is Hidden: " + isHidden);
      Console.WriteLine("All Attributes: " + attributes);
      
      // Remove hidden attribute and clean up
      File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.Hidden);
      File.Delete(filePath);
   }
}

The output of the above code is −

File has been set as hidden.
File: hiddenfile.txt
Is Hidden: True
All Attributes: Hidden, Archive

Complete File Attribute Checker

Example

using System;
using System.IO;

class FileAttributeChecker {
   public static void CheckFileAttributes(string filePath) {
      if (!File.Exists(filePath)) {
         Console.WriteLine("File does not exist: " + filePath);
         return;
      }
      
      FileAttributes attributes = File.GetAttributes(filePath);
      
      Console.WriteLine("File: " + filePath);
      Console.WriteLine("Is Hidden: " + ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden));
      Console.WriteLine("Is ReadOnly: " + ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly));
      Console.WriteLine("Is Directory: " + ((attributes & FileAttributes.Directory) == FileAttributes.Directory));
      Console.WriteLine("Is Archive: " + ((attributes & FileAttributes.Archive) == FileAttributes.Archive));
   }
   
   public static void Main() {
      string testFile = "example.txt";
      
      // Create and test a regular file
      File.WriteAllText(testFile, "Regular file content");
      CheckFileAttributes(testFile);
      
      Console.WriteLine("
--- After setting as hidden ---"); // Make it hidden and test again File.SetAttributes(testFile, File.GetAttributes(testFile) | FileAttributes.Hidden); CheckFileAttributes(testFile); // Clean up File.SetAttributes(testFile, File.GetAttributes(testFile) & ~FileAttributes.Hidden); File.Delete(testFile); } }

The output of the above code is −

File: example.txt
Is Hidden: False
Is ReadOnly: False
Is Directory: False
Is Archive: True

--- After setting as hidden ---
File: example.txt
Is Hidden: True
Is ReadOnly: False
Is Directory: False
Is Archive: True

How It Works

The FileAttributes enumeration uses bitwise flags to represent different file properties. When checking for a specific attribute like Hidden, use the bitwise AND operator & to test if that particular bit is set. The pattern (attributes & FileAttributes.Hidden) == FileAttributes.Hidden returns true if the hidden flag is present.

Conclusion

Checking if a file is hidden in C# involves using File.GetAttributes() to retrieve the file's attributes and then using bitwise operations to test for the FileAttributes.Hidden flag. You can also set or remove the hidden attribute using File.SetAttributes() with appropriate bitwise operations.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements