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.
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
Parameter
Description
path
The encrypted file. It must exist and be on an NTFS drive.
Return Value
Return Value
Description
void
Nothing 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);
}
}
Output
Not supported on this OS
How It Works
Three causes, three messages. Each catch block matches one reason the call can fail, so the user hears the real problem.
Linux and macOS.Decrypt throws PlatformNotSupportedException at once, and the first catch prints the message shown.
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.
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);
}
}
Output
EFS is Windows only. Skipped.
How It Works
The guard decides. On Linux and macOS IsWindows() is false, so only the skip message is printed. That is the output shown.
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.
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);
}
}
Output
secret
How It Works
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.
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.
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.