File.Decrypt Method (String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

Public Shared Sub Decrypt (
	path As String


Type: System.String

A path that describes a file to decrypt.

Exception Condition

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


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.


This operation is not supported on the current platform.


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.


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.


This operation is not supported on the current platform.


The path parameter specified a directory.


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.

Imports System
Imports System.IO
Imports System.Security.AccessControl

Module FileExample

    Sub Main()
            Dim FileName As String = "test.xml"

            Console.WriteLine("Encrypt " + FileName)

            ' Encrypt the file.

            Console.WriteLine("Decrypt " + FileName)

            ' Decrypt the file.

        Catch e As Exception
        End Try


    End Sub

    ' Encrypt a file.
    Sub AddEncryption(ByVal FileName As String)


    End Sub

    ' Decrypt the file.
    Sub RemoveEncryption(ByVal FileName As String)


    End Sub
End Module


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

.NET Framework
Available since 2.0
Return to top