File.Encrypt(String) File.Encrypt(String) File.Encrypt(String) File.Encrypt(String) Method

Definition

Encrypts a file so that only the account used to encrypt the file can decrypt it.

public:
 static void Encrypt(System::String ^ path);
public static void Encrypt (string path);
static member Encrypt : string -> unit
Public Shared Sub Encrypt (path As String)

Parameters

path
String String String String

A path that describes a file to encrypt.

Exceptions

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

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

An I/O error occurred while opening the file.

-or-

This operation is not supported on the current platform.

The specified path, file name, or both exceed the system-defined maximum length.

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.

Examples

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 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.");
    }
}
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);
        }
    }
}
Imports System.IO
Imports System.Security.AccessControl



Module FileExample

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

            Console.WriteLine("Encrypt " + 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

Remarks

The Encrypt method allows you to encrypt a file so that only the account used to call this method can decrypt it. Use the Decrypt method to decrypt a file encrypted by the Encrypt method.

Important

This API is only supported on Windows platforms that are able to use the NTFS Encrypting File System (EFS). Any attempt to use this on non-Windows systems, Windows Home Edition systems, or non-NTFS drives results in a PlatformNotSupportedException or NotSupportedException, depending on the situation.

Use of this API in .NET Core is not recommended; it is included to enable portability for applications that move to .NET Core but still explicitly target Windows.

The Encrypt method requires exclusive access to the file being encrypted, and will fail if another process is using the file.

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.

This method is not available on all versions of Windows. For example, it is not available on Home editions.

The current file system must be formatted as NTFS.

Security

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

Applies to