FileInfo.Encrypt Método

Definición

Cifra un archivo para que solo la cuenta utilizada para cifrar el archivo pueda descifrarlo.

public:
 void Encrypt();
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public void Encrypt();
public void Encrypt();
[System.Runtime.InteropServices.ComVisible(false)]
public void Encrypt();
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.Encrypt : unit -> unit
member this.Encrypt : unit -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Encrypt : unit -> unit
Public Sub Encrypt ()
Atributos

Excepciones

Se especificó una unidad no válida.

No se encontró el archivo descrito por el objeto actual FileInfo .

Error de E/S al abrir el archivo.

El sistema de archivos no es NTFS.

El sistema operativo actual no es Microsoft Windows NT ni posterior.

El archivo descrito por el objeto actual FileInfo es de solo lectura.

O bien

Esta operación no se admite en la plataforma actual.

O bien

El autor de la llamada no tiene el permiso necesario.

Ejemplos

En el ejemplo de código siguiente se usa el Encrypt método y el Decrypt método para cifrar un archivo y, a continuación, descifrarlo.

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
Imports System.IO
Imports System.Security.AccessControl



Module FileExample

    Sub Main()
        Try
            Dim FileName As String = "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 e As Exception
            Console.WriteLine(e)
        End Try

    End Sub



    Sub AddEncryption(ByVal FileName As String)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)
        If fInfo.Exists = False Then
            fInfo.Create()
        End If
        ' Add encryption.
        fInfo.Encrypt()

    End Sub



    Sub RemoveEncryption(ByVal FileName As String)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)
        If fInfo.Exists = False Then
            fInfo.Create()
        End If
        ' Remove encryption.
        fInfo.Decrypt()

    End Sub
End Module
'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

Comentarios

El Encrypt método permite cifrar un archivo para que solo la cuenta utilizada para llamar a este método pueda descifrarlo. Use el Decrypt método para descifrar un archivo cifrado por el Encrypt método .

Tanto el Encrypt método como el Decrypt método usan el proveedor de servicios criptográficos (CSP) instalado en el equipo y las claves de cifrado de archivos del proceso que llama al método .

El sistema de archivos actual debe tener el formato NTFS y el sistema operativo actual debe ser Microsoft Windows NT o posterior.

Se aplica a