C# File Decrypt() Method

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

Definition

In C#, File.Decrypt() removes the NTFS Encrypting File System (EFS) encryption from a file that was protected with File.Encrypt. After it runs, the content is stored in plain form, and any account with normal permission can read it.

Like Encrypt, it is Windows only. Other systems throw PlatformNotSupportedException. It must be run by the account that holds the encryption key.

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

Syntax

Guard the call with OperatingSystem.IsWindows(), and handle IOException and UnauthorizedAccessException.

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

if (OperatingSystem.IsWindows()) {
    File.Decrypt(path);
}
CallWhat it does
File.Decrypt(path)Removes EFS encryption from the file. Windows and NTFS only.
OperatingSystem.IsWindows()true only on Windows. Use it to guard the call.
File.GetAttributes(path).HasFlag(FileAttributes.Encrypted)Tells you whether the file is encrypted right now.

FileInfo.Decrypt() is the object version. Decrypting a file you do not own, or without its key, fails with an access error.

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

Parameters

ParameterDescription
pathThe encrypted file. It must exist and be on an NTFS drive.

Return Value

Return ValueDescription
voidNothing is returned. On success the Encrypted attribute is cleared.

C# Examples

Example 1 — Call Decrypt and Catch the Causes

Call Decrypt directly and handle each typical failure. 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-decrypt");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

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

        try {
            File.Decrypt(path);
            Console.WriteLine("Decrypt 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("Not the owner of the key");
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Three causes, three messages. Each catch block matches one reason the call can fail, so the user hears the real problem.
  2. Linux and macOS. Decrypt throws PlatformNotSupportedException at once, and the first catch prints the message shown.
  3. Windows. With EFS available the call finishes and prints Decrypt call finished. A drive without NTFS or an edition without EFS ends in the IOException branch.
  4. Not the owner. Only the account that holds the key can decrypt. Anyone else gets UnauthorizedAccessException.

Example 2 — A Guarded Encrypt and Decrypt Round Trip

On Windows, encrypt a temp file, check the flag, decrypt, and check again. 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-decrypt-trip");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

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

        if (OperatingSystem.IsWindows()) {
            try {
                File.Encrypt(path);
                Console.WriteLine(File.GetAttributes(path).HasFlag(FileAttributes.Encrypted));
                File.Decrypt(path);
                Console.WriteLine(File.GetAttributes(path).HasFlag(FileAttributes.Encrypted));
            } catch (IOException) {
                Console.WriteLine("EFS unavailable");
            }
        } else {
            Console.WriteLine("EFS is Windows only. Skipped.");
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. The guard decides. On Linux and macOS IsWindows() is false, so only the skip message is printed. That is the output shown.
  2. On Windows with EFS. After Encrypt the Encrypted flag is on. After Decrypt it is off again. The two checks are the proof that the round trip worked.
  3. Everything stays in the temp folder. The file lives in a demo folder that is deleted at the end, so a failed run cannot leave an encrypted file behind in a real folder.

Example 3 — The Owner Reads the Same Content Either Way

Encryption changes how the file is stored, not what the owner sees. The output is the same on every system.

C#
using System;
using System.IO;

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

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

        if (OperatingSystem.IsWindows()) {
            try {
                File.Encrypt(path);
                File.Decrypt(path);
            } catch (IOException) {
                // EFS is not available here; the file is simply left as it is
            }
        }

        Console.WriteLine(File.ReadAllText(path));

        Directory.Delete(root, true);
    }
}

How It Works

  1. The same text in every case. On Windows the file is encrypted and decrypted again. Elsewhere the Windows branch is skipped. Either way the program ends up reading the same file.
  2. The owner never sees ciphertext. EFS encrypts the data on disk, and Windows decrypts it on the fly for the owner. Even while encrypted, ReadAllText would return secret for this account.
  3. An empty catch with a reason. The comment says why nothing happens. In real code, report or log the failure instead of ignoring it.

Frequently Asked Questions

No. It throws PlatformNotSupportedException. EFS is a Windows NTFS feature.
It removes the EFS encryption from a file so the data is stored in plain form. It reverses File.Encrypt.
The account that holds the encryption key, normally the one that called Encrypt. Other accounts get UnauthorizedAccessException.
No. The owner account can read the file normally, and Windows decrypts it on the fly. Decrypt is for removing the encryption itself.
No. File.Decrypt only works with NTFS EFS. Data you encrypted with System.Security.Cryptography must be decrypted with the same algorithm and key.

Next: Delete()

Remove a file from disk. No error if the file is already gone.

Delete() 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.

13 people found this page helpful