File.Decrypt(String) Method

Definition

Decrypts a file that was encrypted by the current account using the Encrypt(String) method.

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static void Decrypt(string path);
C#
public static void Decrypt(string path);

Parameters

path
String

A path that describes a file to decrypt.

Attributes

Exceptions

.NET Framework and .NET Core versions older than 2.1: The path parameter is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

The path parameter is null.

An invalid drive was specified.

The file described by the path parameter could not be found.

An I/O error occurred while opening the file. For example, the encrypted file is already open.

-or-

This operation is not supported on the current platform.

The specified path, file name, or both exceed the system-defined maximum length.

The current operating system is not Windows NT or later.

The file system is not NTFS.

The path parameter specified a file that is read-only.

-or-

This operation is not supported on the current platform.

-or-

The path parameter specified a directory.

-or-

The caller does not have the required permission.

Examples

The following code example uses the Encrypt method and the Decrypt method to encrypt and then decrypt a file. The file must exist for the example to work.

C#
using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string FileName = "test.xml";

                Console.WriteLine("Encrypt " + FileName);

                // Encrypt the file.
                AddEncryption(FileName);

                Console.WriteLine("Decrypt " + FileName);

                // Decrypt the file.
                RemoveEncryption(FileName);

                Console.WriteLine("Done");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }

        // Encrypt a file.
        public static void AddEncryption(string FileName)
        {

            File.Encrypt(FileName);
        }

        // Decrypt a file.
        public static void RemoveEncryption(string FileName)
        {
            File.Decrypt(FileName);
        }
    }
}

Remarks

The Decrypt method allows you to decrypt a file that was encrypted using the Encrypt method. The Decrypt method can decrypt only files that were encrypted using the current user account.

Important

This API is only supported on Windows platforms that are able to use the NTFS Encrypting File System (EFS). Any attempt to use this on non-Windows systems, Windows Home Edition systems, or non-NTFS drives results in a PlatformNotSupportedException or NotSupportedException, depending on the situation.

Use of this API in .NET Core is not recommended; it is included to enable portability for applications that move to .NET Core but still explicitly target Windows.

The Decrypt method requires exclusive access to the file being decrypted, and will raise an exception if another process is using the file.

Both the Encrypt method and the Decrypt method use the cryptographic service provider (CSP) installed on the computer and the file encryption keys of the process calling the method.

The current file system must be formatted as NTFS and the current operating system must be Windows NT or later.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1