Note: This method is new in the .NET Framework version 2.0.
Decrypts a file that was encrypted by the current account using the
Encrypt method.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

Syntax
Visual Basic (Declaration)
Public Shared Sub Decrypt ( _
path As String _
)
Dim path As String
File.Decrypt(path)
public static void Decrypt (
string path
)
public:
static void Decrypt (
String^ path
)
public static void Decrypt (
String path
)
public static function Decrypt (
path : String
)
Parameters
- path
A path that describes a file to decrypt.

Exceptions
| Exception type | Condition |
|---|
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 a null reference (Nothing in Visual Basic). |
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. |
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 Microsoft 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. |

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.
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 Microsoft Windows NT or later.

Example
The following code example uses the Encrypt method and the Decrypt method to encrypt and then decrypt a file.
Imports System
Imports System.IO
Imports System.Security.AccessControl
Module FileExample
Sub Main()
Try
Dim FileName As String = "test.xml"
Console.WriteLine("Enceypt " + FileName)
' Encrypt the file.
AddEncryption(FileName)
Console.WriteLine("Decrypt " + FileName)
' Decrypt the file.
RemoveEncryption(FileName)
Console.WriteLine("Done")
Catch e As Exception
Console.WriteLine(e)
End Try
Console.ReadLine()
End Sub
' Encrypt a file.
Sub AddEncryption(ByVal FileName As String)
File.Encrypt(FileName)
End Sub
' Decrypt the file.
Sub RemoveEncryption(ByVal FileName As String)
File.Decrypt(FileName)
End Sub
End Module
using System;
using System.IO;
using System.Security.AccessControl;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
try
{
string FileName = "test.xml";
Console.WriteLine("Enceypt " + 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);
}
}
}
using namespace System;
using namespace System::IO;
int main()
{
String^ fileName = "test.xml";
if (!File::Exists(fileName))
{
Console::WriteLine("The file " + fileName
+ " does not exist.");
return 0;
}
try
{
Console::WriteLine("Encrypt " + fileName);
// Encrypt the file.
File::Encrypt(fileName);
Console::WriteLine("Decrypt " + fileName);
// Decrypt the file.
File::Decrypt(fileName);
Console::WriteLine("Done");
}
catch (IOException^ ex)
{
Console::WriteLine("There was an IO problem.");
Console::WriteLine(ex->Message);
}
catch (PlatformNotSupportedException^)
{
Console::WriteLine("Encryption is not supported on " +
"this system.");
}
catch (NotSupportedException^)
{
Console::WriteLine("Encryption is not supported on " +
"this system.");
}
catch (UnauthorizedAccessException^)
{
Console::WriteLine("The operation could not be "
+ "carried out.");
}
}

.NET Framework Security
- FileIOPermission
for permission to read and write to the file described by the path parameter. Associated enumerations: Read, Write
Security action: Demand.

Platforms
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information
.NET Framework
Supported in: 2.0

See Also