FileInfo.Decrypt Method
Decrypts a file that was encrypted by the current account using the Encrypt method.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| DriveNotFoundException |
An invalid drive was specified. |
| FileNotFoundException |
The file described by the current FileInfo object could not be found. |
| IOException |
An I/O error occurred while opening the file. |
| NotSupportedException |
The file system is not NTFS. |
| PlatformNotSupportedException |
The current operating system is not Microsoft Windows NT or later. |
| UnauthorizedAccessException |
The file described by the current FileInfo object is read-only. -or- This operation is not supported on the current platform. -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.
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 Microsoft Windows NT or later.
The following code example uses the Encrypt method and the Decrypt method to encrypt and then decrypt a file.
using System; using System.IO; using System.Security.AccessControl; namespace FileSystemExample { class FileExample { public static void Main() { try { string FileName = @"c:\MyTest.txt"; 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); } } public static void AddEncryption(string FileName) { // Create a new FileInfo object. FileInfo fInfo = new FileInfo(FileName); if (!fInfo.Exists) { //Create the file. fInfo.Create(); } // Add encryption. fInfo.Encrypt(); } public static void RemoveEncryption(string FileName) { // Create a new FileInfo object. FileInfo fInfo = new FileInfo(FileName); if (!fInfo.Exists) { //Create the file. fInfo.Create(); } // Remove encryption. fInfo.Decrypt(); } } } //This code produces output similar to the following; //results may vary based on the computer/file structure/etc.: // //Encrypt c:\MyTest.txt //Decrypt c:\MyTest.txt //Done
-
FileIOPermission
Associated enumerations: Read, Write
Security action: Demand.
For permission to read and write to the file described by the current FileInfo object.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.