Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FileInfo::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:
[ComVisibleAttribute(false)]
void Decrypt()

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 namespace System;
using namespace System::IO;
using namespace System::Security::AccessControl;


	static void Addencryption(String^ fileName)
{
    // Create a new FileInfo object.
	FileInfo^ fInfo = gcnew FileInfo(fileName);
	if (!fInfo->Exists)
	{
		fInfo->Create();
	}
	// Add encryption.
    fInfo->Encrypt();

}


	static void Removeencryption(String^ fileName)
{
//    Create a new FileInfo object.
    FileInfo^ fInfo = gcnew FileInfo(fileName);
	if (!fInfo->Exists)
	{
		fInfo->Create();
	}
    // Remove encryption.
    fInfo->Decrypt();
}

int 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 (IOException^ ex)
     {
		Console::WriteLine(ex->Message);
     }
}
//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.

.NET Framework
Available since 2.0
Return to top
Show:
© 2017 Microsoft