C# File Encrypt() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, File.Encrypt() asks Windows to encrypt a file with the NTFS Encrypting File System (EFS). The file is tied to the account that called it, so only that account can read the content. Other accounts get an access error.

This is not a general encryption method. It works only on Windows, on NTFS drives, and in editions that include EFS. On Linux and macOS it throws PlatformNotSupportedException. For protection that works everywhere, use System.Security.Cryptography to encrypt the bytes yourself.

This is a static method of System.IO.File. Call it as File.Encrypt(...); there is no new File(). For an object that remembers one file, see FileInfo.

Syntax

Guard the call with OperatingSystem.IsWindows() and catch IOException for drives or editions without EFS.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-encrypt.txt");
File.WriteAllText(path, "secret");

if (OperatingSystem.IsWindows()) {
    File.Encrypt(path);
}
CallWhat it does
File.Encrypt(path)Turns on EFS encryption for the file. Windows and NTFS only.
OperatingSystem.IsWindows()true only on Windows. Use it as a guard before calling Encrypt.
FileAttributes.EncryptedThe attribute flag that shows a file is encrypted. Read it with GetAttributes on Windows.

FileInfo.Encrypt() is the object version. File.Decrypt reverses the call. Only the owner account sees plain text; a copy to a non-NTFS drive may lose the encryption.

Library declarations
C#
[SupportedOSPlatform("windows")]
public static void Encrypt(string path);

Parameters

ParameterDescription
pathThe file to encrypt. It must exist, be on an NTFS drive, and not be a system or compressed file.

Return Value

Return ValueDescription
voidNothing is returned. On success the file has the Encrypted attribute.

C# Examples

Example 1 — Call Encrypt Only on Windows

Use OperatingSystem.IsWindows() so the program is correct on every system. The output shown is from Linux or macOS.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-encrypt");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "secret.txt");
        File.WriteAllText(path, "secret");

        if (OperatingSystem.IsWindows()) {
            try {
                File.Encrypt(path);
                Console.WriteLine("Encrypted flag: " + File.GetAttributes(path).HasFlag(FileAttributes.Encrypted));
            } catch (IOException) {
                Console.WriteLine("EFS is not available on this drive or edition");
            }
        } else {
            Console.WriteLine("EFS is Windows only. Skipped.");
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Check the system first. OperatingSystem.IsWindows() is false on Linux and macOS, so the program takes the else branch and never calls Encrypt. That is the output shown.
  2. On Windows. The first branch runs. Encrypt asks NTFS to encrypt the file, and the Encrypted attribute is then read back with GetAttributes. Where EFS is not offered, the IOException branch explains it.
  3. Cleanup still works. The folder is deleted in all cases. An encrypted file in your own temp folder can be deleted by the same account.

Example 2 — Catch Each Failure Separately

Call Encrypt directly and handle the three typical exceptions. The output shown is from Linux or macOS.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-encrypt-catch");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "data.txt");
        File.WriteAllText(path, "data");

        try {
            File.Encrypt(path);
            Console.WriteLine("Encrypt call finished");
        } catch (PlatformNotSupportedException) {
            Console.WriteLine("Not supported on this OS");
        } catch (IOException) {
            Console.WriteLine("This drive or edition has no EFS");
        } catch (UnauthorizedAccessException) {
            Console.WriteLine("No permission");
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. A catch for each cause. The three exceptions tell you what to say to the user: wrong operating system, wrong drive or edition, or missing permission.
  2. Order matters. PlatformNotSupportedException and IOException are unrelated, but listing the specific ones first is a good habit.
  3. What you see. On Linux and macOS the first catch runs. On Windows with EFS the call finishes and prints Encrypt call finished. On a Windows edition without EFS, the second catch runs.
  4. Do not hide it. Swallowing every exception silently would leave users thinking the file is protected when it is not.

Example 3 — A Protect Helper That Reports What Happened

Wrap the checks in one method that returns a status text. The output shown is from Linux or macOS.

C#
using System;
using System.IO;

class Program {
    static string Protect(string path) {
        if (!File.Exists(path)) {
            return "Nothing to encrypt";
        }
        if (!OperatingSystem.IsWindows()) {
            return "Skipped: Windows only";
        }
        try {
            File.Encrypt(path);
            return "Encrypted";
        } catch (IOException) {
            return "EFS unavailable";
        }
    }

    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-encrypt-helper");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "card.txt");

        Console.WriteLine(Protect(path));

        File.WriteAllText(path, "1234");
        Console.WriteLine(Protect(path));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Cheapest check first. A missing file returns Nothing to encrypt. This answer is the same on every system.
  2. Then the system check. On Linux and macOS the helper stops with Skipped: Windows only before it touches Encrypt. That is the second line shown.
  3. Only Windows reaches the call. There, Protect returns Encrypted, or EFS unavailable if the drive or edition has no EFS.
  4. One place for the rules. The rest of the program calls Protect and never has to remember the platform rules.

Frequently Asked Questions

No. It throws PlatformNotSupportedException. Use System.Security.Cryptography, for example Aes, to encrypt data on those systems.
The Encrypting File System is a feature of NTFS on Windows. It encrypts a file with a key tied to the user account, so other accounts cannot read it.
The drive may not be NTFS, the Windows edition may not include EFS (for example Windows Home), or the file may be a system or compressed file.
It is a light protection against other accounts on the same PC, tied to Windows. It does not protect the file when the account is signed in, or when copied elsewhere. Use real encryption and a key store for serious secrets.
On Windows, File.GetAttributes(path).HasFlag(FileAttributes.Encrypted) returns true for an EFS-encrypted file.

Next: Exists()

Check whether a file is there. Returns true or false and never throws.

Exists() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful