Export (0) Print
Expand All

File.Decrypt Method

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

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public static void Decrypt(
	string path
)

Parameters

path
Type: System.String

A path that describes a file to decrypt.

ExceptionCondition
ArgumentException

The path parameter is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

ArgumentNullException

The path parameter is null.

DriveNotFoundException

An invalid drive was specified.

FileNotFoundException

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

IOException

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.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

PlatformNotSupportedException

The current operating system is not Windows NT or later.

NotSupportedException

The file system is not NTFS.

UnauthorizedAccessException

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.

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.

The Decrypt method requires exclusive access to the file being decrypted, and will raise an exception if another process is using the file. If the file is not encrypted, Decrypt will return a nonzero value, which indicates success.

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.

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.

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);
        }
    }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft